You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2430 lines
92 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2002-2012 Yves Renard
  4. This file is a part of GETFEM++
  5. Getfem++ is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU Lesser General Public License as published
  7. by the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version along with the GCC Runtime Library
  9. Exception either version 3.1 or (at your option) any later version.
  10. This program is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. License and GCC Runtime Library Exception for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with this program; if not, write to the Free Software Foundation,
  16. Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  17. As a special exception, you may use this file as it is a part of a free
  18. software library without restriction. Specifically, if other files
  19. instantiate templates or use macros or inline functions from this file,
  20. or you compile this file and link it with other files to produce an
  21. executable, this file does not by itself cause the resulting executable
  22. to be covered by the GNU Lesser General Public License. This exception
  23. does not however invalidate any other reasons why the executable file
  24. might be covered by the GNU Lesser General Public License.
  25. ===========================================================================*/
  26. /**@file gmm_blas.h
  27. @author Yves Renard <Yves.Renard@insa-lyon.fr>
  28. @date October 13, 2002.
  29. @brief Basic linear algebra functions.
  30. */
  31. #ifndef GMM_BLAS_H__
  32. #define GMM_BLAS_H__
  33. // This Version of GMM was modified for StoRM
  34. // To detect whether the usage of TBB is possible, this include is neccessary
  35. #include "storm-config.h"
  36. #ifdef STORM_HAVE_INTELTBB
  37. # include <new> // This fixes a potential dependency ordering problem between GMM and TBB
  38. # include "tbb/tbb.h"
  39. # include <iterator>
  40. #endif
  41. #include "gmm_scaled.h"
  42. #include "gmm_transposed.h"
  43. #include "gmm_conjugated.h"
  44. namespace gmm {
  45. /* ******************************************************************** */
  46. /* */
  47. /* Generic algorithms */
  48. /* */
  49. /* ******************************************************************** */
  50. /* ******************************************************************** */
  51. /* Miscellaneous */
  52. /* ******************************************************************** */
  53. /** clear (fill with zeros) a vector or matrix. */
  54. template <typename L> inline void clear(L &l)
  55. { linalg_traits<L>::do_clear(l); }
  56. /** @cond DOXY_SHOW_ALL_FUNCTIONS
  57. skip all these redundant definitions in doxygen documentation..
  58. */
  59. template <typename L> inline void clear(const L &l)
  60. { linalg_traits<L>::do_clear(linalg_const_cast(l)); }
  61. ///@endcond
  62. /** count the number of non-zero entries of a vector or matrix. */ template <typename L> inline size_type nnz(const L& l)
  63. { return nnz(l, typename linalg_traits<L>::linalg_type()); }
  64. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  65. template <typename L> inline size_type nnz(const L& l, abstract_vector) {
  66. typename linalg_traits<L>::const_iterator it = vect_const_begin(l),
  67. ite = vect_const_end(l);
  68. size_type res(0);
  69. for (; it != ite; ++it) ++res;
  70. return res;
  71. }
  72. template <typename L> inline size_type nnz(const L& l, abstract_matrix) {
  73. return nnz(l, typename principal_orientation_type<typename
  74. linalg_traits<L>::sub_orientation>::potype());
  75. }
  76. template <typename L> inline size_type nnz(const L& l, row_major) {
  77. size_type res(0);
  78. for (size_type i = 0; i < mat_nrows(l); ++i)
  79. res += nnz(mat_const_row(l, i));
  80. return res;
  81. }
  82. template <typename L> inline size_type nnz(const L& l, col_major) {
  83. size_type res(0);
  84. for (size_type i = 0; i < mat_ncols(l); ++i)
  85. res += nnz(mat_const_col(l, i));
  86. return res;
  87. }
  88. ///@endcond
  89. /** fill a vector or matrix with x. */
  90. template <typename L> inline
  91. void fill(L& l, typename gmm::linalg_traits<L>::value_type x) {
  92. typedef typename gmm::linalg_traits<L>::value_type T;
  93. if (x == T(0)) gmm::clear(l);
  94. fill(l, x, typename linalg_traits<L>::linalg_type());
  95. }
  96. template <typename L> inline
  97. void fill(const L& l, typename gmm::linalg_traits<L>::value_type x) {
  98. fill(linalg_const_cast(l), x);
  99. }
  100. template <typename L> inline // to be optimized for dense vectors ...
  101. void fill(L& l, typename gmm::linalg_traits<L>::value_type x,
  102. abstract_vector) {
  103. for (size_type i = 0; i < vect_size(l); ++i) l[i] = x;
  104. }
  105. template <typename L> inline // to be optimized for dense matrices ...
  106. void fill(L& l, typename gmm::linalg_traits<L>::value_type x,
  107. abstract_matrix) {
  108. for (size_type i = 0; i < mat_nrows(l); ++i)
  109. for (size_type j = 0; j < mat_ncols(l); ++j)
  110. l(i,j) = x;
  111. }
  112. /** fill a vector or matrix with random value (uniform [-1,1]). */
  113. template <typename L> inline void fill_random(L& l)
  114. { fill_random(l, typename linalg_traits<L>::linalg_type()); }
  115. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  116. template <typename L> inline void fill_random(const L& l) {
  117. fill_random(linalg_const_cast(l),
  118. typename linalg_traits<L>::linalg_type());
  119. }
  120. template <typename L> inline void fill_random(L& l, abstract_vector) {
  121. for (size_type i = 0; i < vect_size(l); ++i)
  122. l[i] = gmm::random(typename linalg_traits<L>::value_type());
  123. }
  124. template <typename L> inline void fill_random(L& l, abstract_matrix) {
  125. for (size_type i = 0; i < mat_nrows(l); ++i)
  126. for (size_type j = 0; j < mat_ncols(l); ++j)
  127. l(i,j) = gmm::random(typename linalg_traits<L>::value_type());
  128. }
  129. ///@endcond
  130. /** fill a vector or matrix with random value.
  131. @param l a vector or matrix.
  132. @param cfill probability of a non-zero value.
  133. */
  134. template <typename L> inline void fill_random(L& l, double cfill)
  135. { fill_random(l, cfill, typename linalg_traits<L>::linalg_type()); }
  136. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  137. template <typename L> inline void fill_random(const L& l, double cfill) {
  138. fill_random(linalg_const_cast(l), cfill,
  139. typename linalg_traits<L>::linalg_type());
  140. }
  141. template <typename L> inline
  142. void fill_random(L& l, double cfill, abstract_vector) {
  143. typedef typename linalg_traits<L>::value_type T;
  144. size_type ntot = std::min(vect_size(l),
  145. size_type(double(vect_size(l))*cfill) + 1);
  146. for (size_type nb = 0; nb < ntot;) {
  147. size_type i = gmm::irandom(vect_size(l));
  148. if (l[i] == T(0)) {
  149. l[i] = gmm::random(typename linalg_traits<L>::value_type());
  150. ++nb;
  151. }
  152. }
  153. }
  154. template <typename L> inline
  155. void fill_random(L& l, double cfill, abstract_matrix) {
  156. fill_random(l, cfill, typename principal_orientation_type<typename
  157. linalg_traits<L>::sub_orientation>::potype());
  158. }
  159. template <typename L> inline
  160. void fill_random(L& l, double cfill, row_major) {
  161. for (size_type i=0; i < mat_nrows(l); ++i) fill_random(mat_row(l,i),cfill);
  162. }
  163. template <typename L> inline
  164. void fill_random(L& l, double cfill, col_major) {
  165. for (size_type j=0; j < mat_ncols(l); ++j) fill_random(mat_col(l,j),cfill);
  166. }
  167. /* resize a vector */
  168. template <typename V> inline
  169. void resize(V &v, size_type n, linalg_false)
  170. { linalg_traits<V>::resize(v, n); }
  171. template <typename V> inline
  172. void resize(V &, size_type , linalg_modifiable)
  173. { GMM_ASSERT1(false, "You cannot resize a reference"); }
  174. template <typename V> inline
  175. void resize(V &, size_type , linalg_const)
  176. { GMM_ASSERT1(false, "You cannot resize a reference"); }
  177. ///@endcond
  178. /** resize a vector. */
  179. template <typename V> inline
  180. void resize(V &v, size_type n) {
  181. resize(v, n, typename linalg_traits<V>::is_reference());
  182. }
  183. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  184. /** resize a matrix **/
  185. template <typename M> inline
  186. void resize(M &v, size_type m, size_type n, linalg_false) {
  187. linalg_traits<M>::resize(v, m, n);
  188. }
  189. template <typename M> inline
  190. void resize(M &, size_type, size_type, linalg_modifiable)
  191. { GMM_ASSERT1(false, "You cannot resize a reference"); }
  192. template <typename M> inline
  193. void resize(M &, size_type, size_type, linalg_const)
  194. { GMM_ASSERT1(false, "You cannot resize a reference"); }
  195. ///@endcond
  196. /** resize a matrix */
  197. template <typename M> inline
  198. void resize(M &v, size_type m, size_type n)
  199. { resize(v, m, n, typename linalg_traits<M>::is_reference()); }
  200. ///@cond
  201. template <typename M> inline
  202. void reshape(M &v, size_type m, size_type n, linalg_false)
  203. { linalg_traits<M>::reshape(v, m, n); }
  204. template <typename M> inline
  205. void reshape(M &, size_type, size_type, linalg_modifiable)
  206. { GMM_ASSERT1(false, "You cannot reshape a reference"); }
  207. template <typename M> inline
  208. void reshape(M &, size_type, size_type, linalg_const)
  209. { GMM_ASSERT1(false, "You cannot reshape a reference"); }
  210. ///@endcond
  211. /** reshape a matrix */
  212. template <typename M> inline
  213. void reshape(M &v, size_type m, size_type n)
  214. { reshape(v, m, n, typename linalg_traits<M>::is_reference()); }
  215. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  216. /* ******************************************************************** */
  217. /* Scalar product */
  218. /* ******************************************************************** */
  219. ///@endcond
  220. /** scalar product between two vectors */
  221. template <typename V1, typename V2> inline
  222. typename strongest_value_type<V1,V2>::value_type
  223. vect_sp(const V1 &v1, const V2 &v2) {
  224. GMM_ASSERT2(vect_size(v1) == vect_size(v2), "dimensions mismatch");
  225. return vect_sp(v1, v2,
  226. typename linalg_traits<V1>::storage_type(),
  227. typename linalg_traits<V2>::storage_type());
  228. }
  229. /** scalar product between two vectors, using a matrix.
  230. @param ps the matrix of the scalar product.
  231. @param v1 the first vector
  232. @param v2 the second vector
  233. */
  234. template <typename MATSP, typename V1, typename V2> inline
  235. typename strongest_value_type3<V1,V2,MATSP>::value_type
  236. vect_sp(const MATSP &ps, const V1 &v1, const V2 &v2) {
  237. return vect_sp_with_mat(ps, v1, v2,
  238. typename linalg_traits<MATSP>::sub_orientation());
  239. }
  240. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  241. template <typename MATSP, typename V1, typename V2> inline
  242. typename strongest_value_type3<V1,V2,MATSP>::value_type
  243. vect_sp_with_mat(const MATSP &ps, const V1 &v1, const V2 &v2, row_major) {
  244. return vect_sp_with_matr(ps, v1, v2,
  245. typename linalg_traits<V2>::storage_type());
  246. }
  247. template <typename MATSP, typename V1, typename V2> inline
  248. typename strongest_value_type3<V1,V2,MATSP>::value_type
  249. vect_sp_with_matr(const MATSP &ps, const V1 &v1, const V2 &v2,
  250. abstract_sparse) {
  251. GMM_ASSERT2(vect_size(v1) == mat_ncols(ps) &&
  252. vect_size(v2) == mat_nrows(ps), "dimensions mismatch");
  253. size_type nr = mat_nrows(ps);
  254. typename linalg_traits<V2>::const_iterator
  255. it = vect_const_begin(v2), ite = vect_const_end(v2);
  256. typename strongest_value_type3<V1,V2,MATSP>::value_type res(0);
  257. for (; it != ite; ++it)
  258. res += vect_sp(mat_const_row(ps, it.index()), v1)* (*it);
  259. return res;
  260. }
  261. template <typename MATSP, typename V1, typename V2> inline
  262. typename strongest_value_type3<V1,V2,MATSP>::value_type
  263. vect_sp_with_matr(const MATSP &ps, const V1 &v1, const V2 &v2,
  264. abstract_skyline)
  265. { return vect_sp_with_matr(ps, v1, v2, abstract_sparse()); }
  266. template <typename MATSP, typename V1, typename V2> inline
  267. typename strongest_value_type3<V1,V2,MATSP>::value_type
  268. vect_sp_with_matr(const MATSP &ps, const V1 &v1, const V2 &v2,
  269. abstract_dense) {
  270. GMM_ASSERT2(vect_size(v1) == mat_ncols(ps) &&
  271. vect_size(v2) == mat_nrows(ps), "dimensions mismatch");
  272. typename linalg_traits<V2>::const_iterator
  273. it = vect_const_begin(v2), ite = vect_const_end(v2);
  274. typename strongest_value_type3<V1,V2,MATSP>::value_type res(0);
  275. for (size_type i = 0; it != ite; ++i, ++it)
  276. res += vect_sp(mat_const_row(ps, i), v1) * (*it);
  277. return res;
  278. }
  279. template <typename MATSP, typename V1, typename V2> inline
  280. typename strongest_value_type3<V1,V2,MATSP>::value_type
  281. vect_sp_with_mat(const MATSP &ps, const V1 &v1,const V2 &v2,row_and_col)
  282. { return vect_sp_with_mat(ps, v1, v2, row_major()); }
  283. template <typename MATSP, typename V1, typename V2> inline
  284. typename strongest_value_type3<V1,V2,MATSP>::value_type
  285. vect_sp_with_mat(const MATSP &ps, const V1 &v1, const V2 &v2,col_major){
  286. return vect_sp_with_matc(ps, v1, v2,
  287. typename linalg_traits<V1>::storage_type());
  288. }
  289. template <typename MATSP, typename V1, typename V2> inline
  290. typename strongest_value_type3<V1,V2,MATSP>::value_type
  291. vect_sp_with_matc(const MATSP &ps, const V1 &v1, const V2 &v2,
  292. abstract_sparse) {
  293. GMM_ASSERT2(vect_size(v1) == mat_ncols(ps) &&
  294. vect_size(v2) == mat_nrows(ps),"dimensions mismatch");
  295. typename linalg_traits<V1>::const_iterator
  296. it = vect_const_begin(v1), ite = vect_const_end(v1);
  297. typename strongest_value_type3<V1,V2,MATSP>::value_type res(0);
  298. for (; it != ite; ++it)
  299. res += vect_sp(mat_const_col(ps, it.index()), v2) * (*it);
  300. return res;
  301. }
  302. template <typename MATSP, typename V1, typename V2> inline
  303. typename strongest_value_type3<V1,V2,MATSP>::value_type
  304. vect_sp_with_matc(const MATSP &ps, const V1 &v1, const V2 &v2,
  305. abstract_skyline)
  306. { return vect_sp_with_matc(ps, v1, v2, abstract_sparse()); }
  307. template <typename MATSP, typename V1, typename V2> inline
  308. typename strongest_value_type3<V1,V2,MATSP>::value_type
  309. vect_sp_with_matc(const MATSP &ps, const V1 &v1, const V2 &v2,
  310. abstract_dense) {
  311. GMM_ASSERT2(vect_size(v1) == mat_ncols(ps) &&
  312. vect_size(v2) == mat_nrows(ps), "dimensions mismatch");
  313. typename linalg_traits<V1>::const_iterator
  314. it = vect_const_begin(v1), ite = vect_const_end(v1);
  315. typename strongest_value_type3<V1,V2,MATSP>::value_type res(0);
  316. for (size_type i = 0; it != ite; ++i, ++it)
  317. res += vect_sp(mat_const_col(ps, i), v2) * (*it);
  318. return res;
  319. }
  320. template <typename MATSP, typename V1, typename V2> inline
  321. typename strongest_value_type3<V1,V2,MATSP>::value_type
  322. vect_sp_with_mat(const MATSP &ps, const V1 &v1,const V2 &v2,col_and_row)
  323. { return vect_sp_with_mat(ps, v1, v2, col_major()); }
  324. template <typename MATSP, typename V1, typename V2> inline
  325. typename strongest_value_type3<V1,V2,MATSP>::value_type
  326. vect_sp_with_mat(const MATSP &ps, const V1 &v1, const V2 &v2,
  327. abstract_null_type) {
  328. typename temporary_vector<V1>::vector_type w(mat_nrows(ps));
  329. GMM_WARNING2("Warning, a temporary is used in scalar product\n");
  330. mult(ps, v1, w);
  331. return vect_sp(w, v2);
  332. }
  333. template <typename IT1, typename IT2> inline
  334. typename strongest_numeric_type<typename std::iterator_traits<IT1>::value_type,
  335. typename std::iterator_traits<IT2>::value_type>::T
  336. vect_sp_dense_(IT1 it, IT1 ite, IT2 it2) {
  337. typename strongest_numeric_type<typename std::iterator_traits<IT1>::value_type,
  338. typename std::iterator_traits<IT2>::value_type>::T res(0);
  339. for (; it != ite; ++it, ++it2) res += (*it) * (*it2);
  340. return res;
  341. }
  342. #ifdef STORM_HAVE_INTELTBB
  343. /* Official Intel Hint on blocked_range vs. linear iterators: http://software.intel.com/en-us/forums/topic/289505
  344. */
  345. template <typename IT1>
  346. class forward_range {
  347. IT1 my_begin;
  348. IT1 my_end;
  349. size_t my_size;
  350. public:
  351. IT1 begin() const {return my_begin;}
  352. IT1 end() const {return my_end;}
  353. bool empty() const {return my_begin==my_end;}
  354. bool is_divisible() const {return my_size>1;}
  355. forward_range( IT1 first, IT1 last, size_t size ) : my_begin(first), my_end(last), my_size(size) {
  356. assert( size==size_t(std::distance( first,last )));
  357. }
  358. forward_range( IT1 first, IT1 last) : my_begin(first), my_end(last) {
  359. my_size = std::distance( first,last );
  360. }
  361. forward_range( forward_range& r, tbb::split ) {
  362. size_t h = r.my_size/2;
  363. my_end = r.my_end;
  364. my_begin = r.my_begin;
  365. std::advance( my_begin, h ); // Might be scaling issue
  366. my_size = r.my_size-h;
  367. r.my_end = my_begin;
  368. r.my_size = h;
  369. }
  370. };
  371. template <typename IT1, typename V>
  372. class tbbHelper_vect_sp_sparse {
  373. V const* my_v;
  374. public:
  375. typename strongest_numeric_type<typename std::iterator_traits<IT1>::value_type,
  376. typename linalg_traits<V>::value_type>::T my_sum;
  377. void operator()( const forward_range<IT1>& r ) {
  378. V const* v = my_v;
  379. typename strongest_numeric_type<typename std::iterator_traits<IT1>::value_type,
  380. typename linalg_traits<V>::value_type>::T sum = my_sum;
  381. IT1 end = r.end();
  382. for( IT1 i=r.begin(); i!=end; ++i) {
  383. sum += (*i) * v->at(i.index());
  384. }
  385. my_sum = sum;
  386. }
  387. tbbHelper_vect_sp_sparse( tbbHelper_vect_sp_sparse& x, tbb::split ) : my_v(x.my_v), my_sum(0) {}
  388. void join( const tbbHelper_vect_sp_sparse& y ) {my_sum+=y.my_sum;}
  389. tbbHelper_vect_sp_sparse(V const* v) :
  390. my_v(v), my_sum(0)
  391. {}
  392. };
  393. #endif
  394. template <typename IT1, typename V> inline
  395. typename strongest_numeric_type<typename std::iterator_traits<IT1>::value_type,
  396. typename linalg_traits<V>::value_type>::T
  397. vect_sp_sparse_(IT1 it, IT1 ite, const V &v) {
  398. typename strongest_numeric_type<typename std::iterator_traits<IT1>::value_type,
  399. typename linalg_traits<V>::value_type>::T res(0);
  400. #if defined(STORM_HAVE_INTELTBB) && defined(STORM_USE_TBB_FOR_INNER)
  401. // This is almost never an efficent way, only if there are _many_ states
  402. tbbHelper_vect_sp_sparse<IT1, V> tbbHelper(&v);
  403. tbb::parallel_reduce(forward_range<IT1>(it, ite), tbbHelper);
  404. res = tbbHelper.my_sum;
  405. #else
  406. for (; it != ite; ++it) res += (*it) * v[it.index()];
  407. #endif
  408. return res;
  409. }
  410. template <typename V1, typename V2> inline
  411. typename strongest_value_type<V1,V2>::value_type
  412. vect_sp(const V1 &v1, const V2 &v2, abstract_dense, abstract_dense) {
  413. return vect_sp_dense_(vect_const_begin(v1), vect_const_end(v1),
  414. vect_const_begin(v2));
  415. }
  416. template <typename V1, typename V2> inline
  417. typename strongest_value_type<V1,V2>::value_type
  418. vect_sp(const V1 &v1, const V2 &v2, abstract_skyline, abstract_dense) {
  419. typename linalg_traits<V1>::const_iterator it1 = vect_const_begin(v1),
  420. ite = vect_const_end(v1);
  421. typename linalg_traits<V2>::const_iterator it2 = vect_const_begin(v2);
  422. return vect_sp_dense_(it1, ite, it2 + it1.index());
  423. }
  424. template <typename V1, typename V2> inline
  425. typename strongest_value_type<V1,V2>::value_type
  426. vect_sp(const V1 &v1, const V2 &v2, abstract_dense, abstract_skyline) {
  427. typename linalg_traits<V2>::const_iterator it1 = vect_const_begin(v2),
  428. ite = vect_const_end(v2);
  429. typename linalg_traits<V1>::const_iterator it2 = vect_const_begin(v1);
  430. return vect_sp_dense_(it1, ite, it2 + it1.index());
  431. }
  432. template <typename V1, typename V2> inline
  433. typename strongest_value_type<V1,V2>::value_type
  434. vect_sp(const V1 &v1, const V2 &v2, abstract_skyline, abstract_skyline) {
  435. typedef typename strongest_value_type<V1,V2>::value_type T;
  436. typename linalg_traits<V1>::const_iterator it1 = vect_const_begin(v1),
  437. ite1 = vect_const_end(v1);
  438. typename linalg_traits<V2>::const_iterator it2 = vect_const_begin(v2),
  439. ite2 = vect_const_end(v2);
  440. size_type n = std::min(ite1.index(), ite2.index());
  441. size_type l = std::max(it1.index(), it2.index());
  442. if (l < n) {
  443. size_type m = l - it1.index(), p = l - it2.index(), q = m + n - l;
  444. return vect_sp_dense_(it1+m, it1+q, it2 + p);
  445. }
  446. return T(0);
  447. }
  448. template <typename V1, typename V2> inline
  449. typename strongest_value_type<V1,V2>::value_type
  450. vect_sp(const V1 &v1, const V2 &v2,abstract_sparse,abstract_dense) {
  451. return vect_sp_sparse_(vect_const_begin(v1), vect_const_end(v1), v2);
  452. }
  453. template <typename V1, typename V2> inline
  454. typename strongest_value_type<V1,V2>::value_type
  455. vect_sp(const V1 &v1, const V2 &v2, abstract_sparse, abstract_skyline) {
  456. return vect_sp_sparse_(vect_const_begin(v1), vect_const_end(v1), v2);
  457. }
  458. template <typename V1, typename V2> inline
  459. typename strongest_value_type<V1,V2>::value_type
  460. vect_sp(const V1 &v1, const V2 &v2, abstract_skyline, abstract_sparse) {
  461. return vect_sp_sparse_(vect_const_begin(v2), vect_const_end(v2), v1);
  462. }
  463. template <typename V1, typename V2> inline
  464. typename strongest_value_type<V1,V2>::value_type
  465. vect_sp(const V1 &v1, const V2 &v2, abstract_dense,abstract_sparse) {
  466. return vect_sp_sparse_(vect_const_begin(v2), vect_const_end(v2), v1);
  467. }
  468. template <typename V1, typename V2> inline
  469. typename strongest_value_type<V1,V2>::value_type
  470. vect_sp_sparse_sparse(const V1 &v1, const V2 &v2, linalg_true) {
  471. typename linalg_traits<V1>::const_iterator it1 = vect_const_begin(v1),
  472. ite1 = vect_const_end(v1);
  473. typename linalg_traits<V2>::const_iterator it2 = vect_const_begin(v2),
  474. ite2 = vect_const_end(v2);
  475. typename strongest_value_type<V1,V2>::value_type res(0);
  476. while (it1 != ite1 && it2 != ite2) {
  477. if (it1.index() == it2.index())
  478. { res += (*it1) * *it2; ++it1; ++it2; }
  479. else if (it1.index() < it2.index()) ++it1; else ++it2;
  480. }
  481. return res;
  482. }
  483. template <typename V1, typename V2> inline
  484. typename strongest_value_type<V1,V2>::value_type
  485. vect_sp_sparse_sparse(const V1 &v1, const V2 &v2, linalg_false) {
  486. return vect_sp_sparse_(vect_const_begin(v1), vect_const_end(v1), v2);
  487. }
  488. template <typename V1, typename V2> inline
  489. typename strongest_value_type<V1,V2>::value_type
  490. vect_sp(const V1 &v1, const V2 &v2,abstract_sparse,abstract_sparse) {
  491. return vect_sp_sparse_sparse(v1, v2,
  492. typename linalg_and<typename linalg_traits<V1>::index_sorted,
  493. typename linalg_traits<V2>::index_sorted>::bool_type());
  494. }
  495. /* ******************************************************************** */
  496. /* Hermitian product */
  497. /* ******************************************************************** */
  498. ///@endcond
  499. /** Hermitian product. */
  500. template <typename V1, typename V2>
  501. inline typename strongest_value_type<V1,V2>::value_type
  502. vect_hp(const V1 &v1, const V2 &v2)
  503. { return vect_sp(v1, conjugated(v2)); }
  504. /** Hermitian product with a matrix. */
  505. template <typename MATSP, typename V1, typename V2> inline
  506. typename strongest_value_type3<V1,V2,MATSP>::value_type
  507. vect_hp(const MATSP &ps, const V1 &v1, const V2 &v2) {
  508. return vect_sp(ps, v1, gmm::conjugated(v2));
  509. }
  510. /* ******************************************************************** */
  511. /* Trace of a matrix */
  512. /* ******************************************************************** */
  513. /** Trace of a matrix */
  514. template <typename M>
  515. typename linalg_traits<M>::value_type
  516. mat_trace(const M &m) {
  517. typedef typename linalg_traits<M>::value_type T;
  518. T res(0);
  519. for (size_type i = 0; i < std::min(mat_nrows(m), mat_ncols(m)); ++i)
  520. res += m(i,i);
  521. return res;
  522. }
  523. /* ******************************************************************** */
  524. /* Euclidean norm */
  525. /* ******************************************************************** */
  526. /** Euclidean norm of a vector. */
  527. template <typename V>
  528. typename number_traits<typename linalg_traits<V>::value_type>
  529. ::magnitude_type
  530. vect_norm2_sqr(const V &v) {
  531. typedef typename linalg_traits<V>::value_type T;
  532. typedef typename number_traits<T>::magnitude_type R;
  533. typename linalg_traits<V>::const_iterator
  534. it = vect_const_begin(v), ite = vect_const_end(v);
  535. R res(0);
  536. for (; it != ite; ++it) res += gmm::abs_sqr(*it);
  537. return res;
  538. }
  539. /** squared Euclidean norm of a vector. */
  540. template <typename V> inline
  541. typename number_traits<typename linalg_traits<V>::value_type>
  542. ::magnitude_type
  543. vect_norm2(const V &v)
  544. { return sqrt(vect_norm2_sqr(v)); }
  545. /** squared Euclidean distance between two vectors */
  546. template <typename V1, typename V2> inline
  547. typename number_traits<typename linalg_traits<V1>::value_type>
  548. ::magnitude_type
  549. vect_dist2_sqr(const V1 &v1, const V2 &v2) { // not fully optimized
  550. typedef typename linalg_traits<V1>::value_type T;
  551. typedef typename number_traits<T>::magnitude_type R;
  552. typename linalg_traits<V1>::const_iterator
  553. it1 = vect_const_begin(v1), ite1 = vect_const_end(v1);
  554. typename linalg_traits<V2>::const_iterator
  555. it2 = vect_const_begin(v2), ite2 = vect_const_end(v2);
  556. size_type k1(0), k2(0);
  557. R res(0);
  558. while (it1 != ite1 && it2 != ite2) {
  559. size_type i1 = index_of_it(it1, k1,
  560. typename linalg_traits<V1>::storage_type());
  561. size_type i2 = index_of_it(it2, k2,
  562. typename linalg_traits<V2>::storage_type());
  563. if (i1 == i2) {
  564. res += gmm::abs_sqr(*it2 - *it1); ++it1; ++k1; ++it2; ++k2;
  565. }
  566. else if (i1 < i2) {
  567. res += gmm::abs_sqr(*it1); ++it1; ++k1;
  568. }
  569. else {
  570. res += gmm::abs_sqr(*it2); ++it2; ++k2;
  571. }
  572. }
  573. while (it1 != ite1) { res += gmm::abs_sqr(*it1); ++it1; }
  574. while (it2 != ite2) { res += gmm::abs_sqr(*it2); ++it2; }
  575. return res;
  576. }
  577. /** Euclidean distance between two vectors */
  578. template <typename V1, typename V2> inline
  579. typename number_traits<typename linalg_traits<V1>::value_type>
  580. ::magnitude_type
  581. vect_dist2(const V1 &v1, const V2 &v2)
  582. { return sqrt(vect_dist2_sqr(v1, v2)); }
  583. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  584. template <typename M>
  585. typename number_traits<typename linalg_traits<M>::value_type>
  586. ::magnitude_type
  587. mat_euclidean_norm_sqr(const M &m, row_major) {
  588. typename number_traits<typename linalg_traits<M>::value_type>
  589. ::magnitude_type res(0);
  590. for (size_type i = 0; i < mat_nrows(m); ++i)
  591. res += vect_norm2_sqr(mat_const_row(m, i));
  592. return res;
  593. }
  594. template <typename M>
  595. typename number_traits<typename linalg_traits<M>::value_type>
  596. ::magnitude_type
  597. mat_euclidean_norm_sqr(const M &m, col_major) {
  598. typename number_traits<typename linalg_traits<M>::value_type>
  599. ::magnitude_type res(0);
  600. for (size_type i = 0; i < mat_ncols(m); ++i)
  601. res += vect_norm2_sqr(mat_const_col(m, i));
  602. return res;
  603. }
  604. ///@endcond
  605. /** squared Euclidean norm of a matrix. */
  606. template <typename M> inline
  607. typename number_traits<typename linalg_traits<M>::value_type>
  608. ::magnitude_type
  609. mat_euclidean_norm_sqr(const M &m) {
  610. return mat_euclidean_norm_sqr(m,
  611. typename principal_orientation_type<typename
  612. linalg_traits<M>::sub_orientation>::potype());
  613. }
  614. /** Euclidean norm of a matrix. */
  615. template <typename M> inline
  616. typename number_traits<typename linalg_traits<M>::value_type>
  617. ::magnitude_type
  618. mat_euclidean_norm(const M &m)
  619. { return gmm::sqrt(mat_euclidean_norm_sqr(m)); }
  620. /* ******************************************************************** */
  621. /* vector norm1 */
  622. /* ******************************************************************** */
  623. /** 1-norm of a vector */
  624. template <typename V>
  625. typename number_traits<typename linalg_traits<V>::value_type>
  626. ::magnitude_type
  627. vect_norm1(const V &v) {
  628. typename linalg_traits<V>::const_iterator
  629. it = vect_const_begin(v), ite = vect_const_end(v);
  630. typename number_traits<typename linalg_traits<V>::value_type>
  631. ::magnitude_type res(0);
  632. for (; it != ite; ++it) res += gmm::abs(*it);
  633. return res;
  634. }
  635. /* ******************************************************************** */
  636. /* vector Infinity norm */
  637. /* ******************************************************************** */
  638. /** Infinity norm of a vector. */
  639. template <typename V>
  640. typename number_traits<typename linalg_traits<V>::value_type>
  641. ::magnitude_type
  642. vect_norminf(const V &v) {
  643. typename linalg_traits<V>::const_iterator
  644. it = vect_const_begin(v), ite = vect_const_end(v);
  645. typename number_traits<typename linalg_traits<V>::value_type>
  646. ::magnitude_type res(0);
  647. for (; it != ite; ++it) res = std::max(res, gmm::abs(*it));
  648. return res;
  649. }
  650. /* ******************************************************************** */
  651. /* matrix norm1 */
  652. /* ******************************************************************** */
  653. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  654. template <typename M>
  655. typename number_traits<typename linalg_traits<M>::value_type>
  656. ::magnitude_type
  657. mat_norm1(const M &m, col_major) {
  658. typename number_traits<typename linalg_traits<M>::value_type>
  659. ::magnitude_type res(0);
  660. for (size_type i = 0; i < mat_ncols(m); ++i)
  661. res = std::max(res, vect_norm1(mat_const_col(m,i)));
  662. return res;
  663. }
  664. template <typename M>
  665. typename number_traits<typename linalg_traits<M>::value_type>
  666. ::magnitude_type
  667. mat_norm1(const M &m, row_major) {
  668. typedef typename linalg_traits<M>::value_type T;
  669. typedef typename number_traits<T>::magnitude_type R;
  670. typedef typename linalg_traits<M>::storage_type store_type;
  671. std::vector<R> aux(mat_ncols(m));
  672. for (size_type i = 0; i < mat_nrows(m); ++i) {
  673. typedef typename linalg_traits<M>::const_sub_row_type row_type;
  674. row_type row = mat_const_row(m, i);
  675. typename linalg_traits<row_type>::const_iterator
  676. it = vect_const_begin(row), ite = vect_const_end(row);
  677. for (size_type k = 0; it != ite; ++it, ++k)
  678. aux[index_of_it(it, k, store_type())] += gmm::abs(*it);
  679. }
  680. return vect_norminf(aux);
  681. }
  682. template <typename M>
  683. typename number_traits<typename linalg_traits<M>::value_type>
  684. ::magnitude_type
  685. mat_norm1(const M &m, col_and_row)
  686. { return mat_norm1(m, col_major()); }
  687. template <typename M>
  688. typename number_traits<typename linalg_traits<M>::value_type>
  689. ::magnitude_type
  690. mat_norm1(const M &m, row_and_col)
  691. { return mat_norm1(m, col_major()); }
  692. ///@endcond
  693. /** 1-norm of a matrix */
  694. template <typename M>
  695. typename number_traits<typename linalg_traits<M>::value_type>
  696. ::magnitude_type
  697. mat_norm1(const M &m) {
  698. return mat_norm1(m, typename linalg_traits<M>::sub_orientation());
  699. }
  700. /* ******************************************************************** */
  701. /* matrix Infinity norm */
  702. /* ******************************************************************** */
  703. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  704. template <typename M>
  705. typename number_traits<typename linalg_traits<M>::value_type>
  706. ::magnitude_type
  707. mat_norminf(const M &m, row_major) {
  708. typename number_traits<typename linalg_traits<M>::value_type>
  709. ::magnitude_type res(0);
  710. for (size_type i = 0; i < mat_nrows(m); ++i)
  711. res = std::max(res, vect_norm1(mat_const_row(m,i)));
  712. return res;
  713. }
  714. template <typename M>
  715. typename number_traits<typename linalg_traits<M>::value_type>
  716. ::magnitude_type
  717. mat_norminf(const M &m, col_major) {
  718. typedef typename linalg_traits<M>::value_type T;
  719. typedef typename number_traits<T>::magnitude_type R;
  720. typedef typename linalg_traits<M>::storage_type store_type;
  721. std::vector<R> aux(mat_nrows(m));
  722. for (size_type i = 0; i < mat_ncols(m); ++i) {
  723. typedef typename linalg_traits<M>::const_sub_col_type col_type;
  724. col_type col = mat_const_col(m, i);
  725. typename linalg_traits<col_type>::const_iterator
  726. it = vect_const_begin(col), ite = vect_const_end(col);
  727. for (size_type k = 0; it != ite; ++it, ++k)
  728. aux[index_of_it(it, k, store_type())] += gmm::abs(*it);
  729. }
  730. return vect_norminf(aux);
  731. }
  732. template <typename M>
  733. typename number_traits<typename linalg_traits<M>::value_type>
  734. ::magnitude_type
  735. mat_norminf(const M &m, col_and_row)
  736. { return mat_norminf(m, row_major()); }
  737. template <typename M>
  738. typename number_traits<typename linalg_traits<M>::value_type>
  739. ::magnitude_type
  740. mat_norminf(const M &m, row_and_col)
  741. { return mat_norminf(m, row_major()); }
  742. ///@endcond
  743. /** infinity-norm of a matrix.*/
  744. template <typename M>
  745. typename number_traits<typename linalg_traits<M>::value_type>
  746. ::magnitude_type
  747. mat_norminf(const M &m) {
  748. return mat_norminf(m, typename linalg_traits<M>::sub_orientation());
  749. }
  750. /* ******************************************************************** */
  751. /* Max norm for matrices */
  752. /* ******************************************************************** */
  753. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  754. template <typename M>
  755. typename number_traits<typename linalg_traits<M>::value_type>
  756. ::magnitude_type
  757. mat_maxnorm(const M &m, row_major) {
  758. typename number_traits<typename linalg_traits<M>::value_type>
  759. ::magnitude_type res(0);
  760. for (size_type i = 0; i < mat_nrows(m); ++i)
  761. res = std::max(res, vect_norminf(mat_const_row(m,i)));
  762. return res;
  763. }
  764. template <typename M>
  765. typename number_traits<typename linalg_traits<M>::value_type>
  766. ::magnitude_type
  767. mat_maxnorm(const M &m, col_major) {
  768. typename number_traits<typename linalg_traits<M>::value_type>
  769. ::magnitude_type res(0);
  770. for (size_type i = 0; i < mat_ncols(m); ++i)
  771. res = std::max(res, vect_norminf(mat_const_col(m,i)));
  772. return res;
  773. }
  774. ///@endcond
  775. /** max-norm of a matrix. */
  776. template <typename M>
  777. typename number_traits<typename linalg_traits<M>::value_type>
  778. ::magnitude_type
  779. mat_maxnorm(const M &m) {
  780. return mat_maxnorm(m,
  781. typename principal_orientation_type<typename
  782. linalg_traits<M>::sub_orientation>::potype());
  783. }
  784. /* ******************************************************************** */
  785. /* Clean */
  786. /* ******************************************************************** */
  787. /** Clean a vector or matrix (replace near-zero entries with zeroes). */
  788. template <typename L> inline void clean(L &l, double threshold);
  789. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  790. template <typename L, typename T>
  791. void clean(L &l, double threshold, abstract_dense, T) {
  792. typedef typename number_traits<T>::magnitude_type R;
  793. typename linalg_traits<L>::iterator it = vect_begin(l), ite = vect_end(l);
  794. for (; it != ite; ++it)
  795. if (gmm::abs(*it) < R(threshold)) *it = T(0);
  796. }
  797. template <typename L, typename T>
  798. void clean(L &l, double threshold, abstract_skyline, T)
  799. { gmm::clean(l, threshold, abstract_dense(), T()); }
  800. template <typename L, typename T>
  801. void clean(L &l, double threshold, abstract_sparse, T) {
  802. typedef typename number_traits<T>::magnitude_type R;
  803. typename linalg_traits<L>::iterator it = vect_begin(l), ite = vect_end(l);
  804. std::vector<size_type> ind;
  805. for (; it != ite; ++it)
  806. if (gmm::abs(*it) < R(threshold)) ind.push_back(it.index());
  807. for (size_type i = 0; i < ind.size(); ++i) l[ind[i]] = T(0);
  808. }
  809. template <typename L, typename T>
  810. void clean(L &l, double threshold, abstract_dense, std::complex<T>) {
  811. typename linalg_traits<L>::iterator it = vect_begin(l), ite = vect_end(l);
  812. for (; it != ite; ++it){
  813. if (gmm::abs((*it).real()) < T(threshold))
  814. *it = std::complex<T>(T(0), (*it).imag());
  815. if (gmm::abs((*it).imag()) < T(threshold))
  816. *it = std::complex<T>((*it).real(), T(0));
  817. }
  818. }
  819. template <typename L, typename T>
  820. void clean(L &l, double threshold, abstract_skyline, std::complex<T>)
  821. { gmm::clean(l, threshold, abstract_dense(), std::complex<T>()); }
  822. template <typename L, typename T>
  823. void clean(L &l, double threshold, abstract_sparse, std::complex<T>) {
  824. typename linalg_traits<L>::iterator it = vect_begin(l), ite = vect_end(l);
  825. std::vector<size_type> ind;
  826. for (; it != ite; ++it) {
  827. bool r = (gmm::abs((*it).real()) < T(threshold));
  828. bool i = (gmm::abs((*it).imag()) < T(threshold));
  829. if (r && i) ind.push_back(it.index());
  830. else if (r) (*it).real() = T(0);
  831. else if (i) (*it).imag() = T(0);
  832. }
  833. for (size_type i = 0; i < ind.size(); ++i)
  834. l[ind[i]] = std::complex<T>(T(0),T(0));
  835. }
  836. template <typename L> inline void clean(L &l, double threshold,
  837. abstract_vector) {
  838. gmm::clean(l, threshold, typename linalg_traits<L>::storage_type(),
  839. typename linalg_traits<L>::value_type());
  840. }
  841. template <typename L> inline void clean(const L &l, double threshold);
  842. template <typename L> void clean(L &l, double threshold, row_major) {
  843. for (size_type i = 0; i < mat_nrows(l); ++i)
  844. gmm::clean(mat_row(l, i), threshold);
  845. }
  846. template <typename L> void clean(L &l, double threshold, col_major) {
  847. for (size_type i = 0; i < mat_ncols(l); ++i)
  848. gmm::clean(mat_col(l, i), threshold);
  849. }
  850. template <typename L> inline void clean(L &l, double threshold,
  851. abstract_matrix) {
  852. gmm::clean(l, threshold,
  853. typename principal_orientation_type<typename
  854. linalg_traits<L>::sub_orientation>::potype());
  855. }
  856. template <typename L> inline void clean(L &l, double threshold)
  857. { clean(l, threshold, typename linalg_traits<L>::linalg_type()); }
  858. template <typename L> inline void clean(const L &l, double threshold)
  859. { gmm::clean(linalg_const_cast(l), threshold); }
  860. /* ******************************************************************** */
  861. /* Copy */
  862. /* ******************************************************************** */
  863. ///@endcond
  864. /** Copy vectors or matrices.
  865. @param l1 source vector or matrix.
  866. @param l2 destination.
  867. */
  868. template <typename L1, typename L2> inline
  869. void copy(const L1& l1, L2& l2) {
  870. if ((const void *)(&l1) != (const void *)(&l2)) {
  871. if (same_origin(l1,l2))
  872. GMM_WARNING2("Warning : a conflict is possible in copy\n");
  873. copy(l1, l2, typename linalg_traits<L1>::linalg_type(),
  874. typename linalg_traits<L2>::linalg_type());
  875. }
  876. }
  877. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  878. template <typename L1, typename L2> inline
  879. void copy(const L1& l1, const L2& l2) { copy(l1, linalg_const_cast(l2)); }
  880. template <typename L1, typename L2> inline
  881. void copy(const L1& l1, L2& l2, abstract_vector, abstract_vector) {
  882. GMM_ASSERT2(vect_size(l1) == vect_size(l2), "dimensions mismatch");
  883. copy_vect(l1, l2, typename linalg_traits<L1>::storage_type(),
  884. typename linalg_traits<L2>::storage_type());
  885. }
  886. template <typename L1, typename L2> inline
  887. void copy(const L1& l1, L2& l2, abstract_matrix, abstract_matrix) {
  888. size_type m = mat_nrows(l1), n = mat_ncols(l1);
  889. if (!m || !n) return;
  890. GMM_ASSERT2(n==mat_ncols(l2) && m==mat_nrows(l2), "dimensions mismatch");
  891. copy_mat(l1, l2, typename linalg_traits<L1>::sub_orientation(),
  892. typename linalg_traits<L2>::sub_orientation());
  893. }
  894. template <typename V1, typename V2, typename C1, typename C2> inline
  895. void copy_vect(const V1 &v1, const V2 &v2, C1, C2)
  896. { copy_vect(v1, const_cast<V2 &>(v2), C1(), C2()); }
  897. template <typename L1, typename L2>
  898. void copy_mat_by_row(const L1& l1, L2& l2) {
  899. size_type nbr = mat_nrows(l1);
  900. for (size_type i = 0; i < nbr; ++i)
  901. copy_vect(mat_const_row(l1, i), mat_row(l2, i),
  902. typename linalg_traits<L1>::storage_type(),
  903. typename linalg_traits<L2>::storage_type());
  904. }
  905. template <typename L1, typename L2>
  906. void copy_mat_by_col(const L1 &l1, L2 &l2) {
  907. size_type nbc = mat_ncols(l1);
  908. for (size_type i = 0; i < nbc; ++i) {
  909. copy_vect(mat_const_col(l1, i), mat_col(l2, i),
  910. typename linalg_traits<L1>::storage_type(),
  911. typename linalg_traits<L2>::storage_type());
  912. }
  913. }
  914. template <typename L1, typename L2> inline
  915. void copy_mat(const L1& l1, L2& l2, row_major, row_major)
  916. { copy_mat_by_row(l1, l2); }
  917. template <typename L1, typename L2> inline
  918. void copy_mat(const L1& l1, L2& l2, row_major, row_and_col)
  919. { copy_mat_by_row(l1, l2); }
  920. template <typename L1, typename L2> inline
  921. void copy_mat(const L1& l1, L2& l2, row_and_col, row_and_col)
  922. { copy_mat_by_row(l1, l2); }
  923. template <typename L1, typename L2> inline
  924. void copy_mat(const L1& l1, L2& l2, row_and_col, row_major)
  925. { copy_mat_by_row(l1, l2); }
  926. template <typename L1, typename L2> inline
  927. void copy_mat(const L1& l1, L2& l2, col_and_row, row_major)
  928. { copy_mat_by_row(l1, l2); }
  929. template <typename L1, typename L2> inline
  930. void copy_mat(const L1& l1, L2& l2, row_major, col_and_row)
  931. { copy_mat_by_row(l1, l2); }
  932. template <typename L1, typename L2> inline
  933. void copy_mat(const L1& l1, L2& l2, col_and_row, row_and_col)
  934. { copy_mat_by_row(l1, l2); }
  935. template <typename L1, typename L2> inline
  936. void copy_mat(const L1& l1, L2& l2, row_and_col, col_and_row)
  937. { copy_mat_by_row(l1, l2); }
  938. template <typename L1, typename L2> inline
  939. void copy_mat(const L1& l1, L2& l2, col_major, col_major)
  940. { copy_mat_by_col(l1, l2); }
  941. template <typename L1, typename L2> inline
  942. void copy_mat(const L1& l1, L2& l2, col_major, col_and_row)
  943. { copy_mat_by_col(l1, l2); }
  944. template <typename L1, typename L2> inline
  945. void copy_mat(const L1& l1, L2& l2, col_major, row_and_col)
  946. { copy_mat_by_col(l1, l2); }
  947. template <typename L1, typename L2> inline
  948. void copy_mat(const L1& l1, L2& l2, row_and_col, col_major)
  949. { copy_mat_by_col(l1, l2); }
  950. template <typename L1, typename L2> inline
  951. void copy_mat(const L1& l1, L2& l2, col_and_row, col_major)
  952. { copy_mat_by_col(l1, l2); }
  953. template <typename L1, typename L2> inline
  954. void copy_mat(const L1& l1, L2& l2, col_and_row, col_and_row)
  955. { copy_mat_by_col(l1, l2); }
  956. template <typename L1, typename L2> inline
  957. void copy_mat_mixed_rc(const L1& l1, L2& l2, size_type i) {
  958. copy_mat_mixed_rc(l1, l2, i, typename linalg_traits<L1>::storage_type());
  959. }
  960. template <typename L1, typename L2>
  961. void copy_mat_mixed_rc(const L1& l1, L2& l2, size_type i, abstract_sparse) {
  962. typename linalg_traits<L1>::const_iterator
  963. it = vect_const_begin(l1), ite = vect_const_end(l1);
  964. for (; it != ite; ++it)
  965. l2(i, it.index()) = *it;
  966. }
  967. template <typename L1, typename L2>
  968. void copy_mat_mixed_rc(const L1& l1, L2& l2, size_type i, abstract_skyline) {
  969. typename linalg_traits<L1>::const_iterator
  970. it = vect_const_begin(l1), ite = vect_const_end(l1);
  971. for (; it != ite; ++it)
  972. l2(i, it.index()) = *it;
  973. }
  974. template <typename L1, typename L2>
  975. void copy_mat_mixed_rc(const L1& l1, L2& l2, size_type i, abstract_dense) {
  976. typename linalg_traits<L1>::const_iterator
  977. it = vect_const_begin(l1), ite = vect_const_end(l1);
  978. for (size_type j = 0; it != ite; ++it, ++j) l2(i, j) = *it;
  979. }
  980. template <typename L1, typename L2> inline
  981. void copy_mat_mixed_cr(const L1& l1, L2& l2, size_type i) {
  982. copy_mat_mixed_cr(l1, l2, i, typename linalg_traits<L1>::storage_type());
  983. }
  984. template <typename L1, typename L2>
  985. void copy_mat_mixed_cr(const L1& l1, L2& l2, size_type i, abstract_sparse) {
  986. typename linalg_traits<L1>::const_iterator
  987. it = vect_const_begin(l1), ite = vect_const_end(l1);
  988. for (; it != ite; ++it) l2(it.index(), i) = *it;
  989. }
  990. template <typename L1, typename L2>
  991. void copy_mat_mixed_cr(const L1& l1, L2& l2, size_type i, abstract_skyline) {
  992. typename linalg_traits<L1>::const_iterator
  993. it = vect_const_begin(l1), ite = vect_const_end(l1);
  994. for (; it != ite; ++it) l2(it.index(), i) = *it;
  995. }
  996. template <typename L1, typename L2>
  997. void copy_mat_mixed_cr(const L1& l1, L2& l2, size_type i, abstract_dense) {
  998. typename linalg_traits<L1>::const_iterator
  999. it = vect_const_begin(l1), ite = vect_const_end(l1);
  1000. for (size_type j = 0; it != ite; ++it, ++j) l2(j, i) = *it;
  1001. }
  1002. template <typename L1, typename L2>
  1003. void copy_mat(const L1& l1, L2& l2, row_major, col_major) {
  1004. clear(l2);
  1005. size_type nbr = mat_nrows(l1);
  1006. for (size_type i = 0; i < nbr; ++i)
  1007. copy_mat_mixed_rc(mat_const_row(l1, i), l2, i);
  1008. }
  1009. template <typename L1, typename L2>
  1010. void copy_mat(const L1& l1, L2& l2, col_major, row_major) {
  1011. clear(l2);
  1012. size_type nbc = mat_ncols(l1);
  1013. for (size_type i = 0; i < nbc; ++i)
  1014. copy_mat_mixed_cr(mat_const_col(l1, i), l2, i);
  1015. }
  1016. template <typename L1, typename L2> inline
  1017. void copy_vect(const L1 &l1, L2 &l2, abstract_dense, abstract_dense) {
  1018. std::copy(vect_const_begin(l1), vect_const_end(l1), vect_begin(l2));
  1019. }
  1020. template <typename L1, typename L2> inline // to be optimised ?
  1021. void copy_vect(const L1 &l1, L2 &l2, abstract_skyline, abstract_skyline) {
  1022. typename linalg_traits<L1>::const_iterator it1 = vect_const_begin(l1),
  1023. ite1 = vect_const_end(l1);
  1024. while (it1 != ite1 && *it1 == typename linalg_traits<L1>::value_type(0))
  1025. ++it1;
  1026. if (ite1 - it1 > 0) {
  1027. clear(l2);
  1028. typename linalg_traits<L2>::iterator it2 = vect_begin(l2),
  1029. ite2 = vect_end(l2);
  1030. while (*(ite1-1) == typename linalg_traits<L1>::value_type(0)) ite1--;
  1031. if (it2 == ite2) {
  1032. l2[it1.index()] = *it1; ++it1;
  1033. l2[ite1.index()-1] = *(ite1-1); --ite1;
  1034. if (it1 < ite1)
  1035. { it2 = vect_begin(l2); ++it2; std::copy(it1, ite1, it2); }
  1036. }
  1037. else {
  1038. ptrdiff_t m = it1.index() - it2.index();
  1039. if (m >= 0 && ite1.index() <= ite2.index())
  1040. std::copy(it1, ite1, it2 + m);
  1041. else {
  1042. if (m < 0) l2[it1.index()] = *it1;
  1043. if (ite1.index() > ite2.index()) l2[ite1.index()-1] = *(ite1-1);
  1044. it2 = vect_begin(l2); ite2 = vect_end(l2);
  1045. m = it1.index() - it2.index();
  1046. std::copy(it1, ite1, it2 + m);
  1047. }
  1048. }
  1049. }
  1050. }
  1051. template <typename L1, typename L2>
  1052. void copy_vect(const L1& l1, L2& l2, abstract_sparse, abstract_dense) {
  1053. clear(l2);
  1054. typename linalg_traits<L1>::const_iterator
  1055. it = vect_const_begin(l1), ite = vect_const_end(l1);
  1056. for (; it != ite; ++it) { l2[it.index()] = *it; }
  1057. }
  1058. template <typename L1, typename L2>
  1059. void copy_vect(const L1& l1, L2& l2, abstract_sparse, abstract_skyline) {
  1060. clear(l2);
  1061. typename linalg_traits<L1>::const_iterator
  1062. it = vect_const_begin(l1), ite = vect_const_end(l1);
  1063. for (; it != ite; ++it) l2[it.index()] = *it;
  1064. }
  1065. template <typename L1, typename L2>
  1066. void copy_vect(const L1& l1, L2& l2, abstract_skyline, abstract_dense) {
  1067. typedef typename linalg_traits<L1>::value_type T;
  1068. typedef typename linalg_traits<L1>::const_iterator l1_const_iterator;
  1069. typedef typename linalg_traits<L2>::iterator l2_iterator;
  1070. l1_const_iterator it = vect_const_begin(l1), ite = vect_const_end(l1);
  1071. if (it == ite)
  1072. gmm::clear(l2);
  1073. else {
  1074. l2_iterator it2 = vect_begin(l2), ite2 = vect_end(l2);
  1075. size_type i = it.index(), j;
  1076. for (j = 0; j < i; ++j, ++it2) *it2 = T(0);
  1077. for (; it != ite; ++it, ++it2) *it2 = *it;
  1078. for (; it2 != ite2; ++it2) *it2 = T(0);
  1079. }
  1080. }
  1081. template <typename L1, typename L2>
  1082. void copy_vect(const L1& l1, L2& l2, abstract_sparse, abstract_sparse) {
  1083. typename linalg_traits<L1>::const_iterator
  1084. it = vect_const_begin(l1), ite = vect_const_end(l1);
  1085. clear(l2);
  1086. for (; it != ite; ++it)
  1087. if (*it != (typename linalg_traits<L1>::value_type)(0))
  1088. l2[it.index()] = *it;
  1089. }
  1090. template <typename L1, typename L2>
  1091. void copy_vect(const L1& l1, L2& l2, abstract_dense, abstract_sparse) {
  1092. clear(l2);
  1093. typename linalg_traits<L1>::const_iterator
  1094. it = vect_const_begin(l1), ite = vect_const_end(l1);
  1095. for (size_type i = 0; it != ite; ++it, ++i)
  1096. if (*it != (typename linalg_traits<L1>::value_type)(0))
  1097. l2[i] = *it;
  1098. }
  1099. template <typename L1, typename L2> // to be optimised ...
  1100. void copy_vect(const L1& l1, L2& l2, abstract_dense, abstract_skyline) {
  1101. clear(l2);
  1102. typename linalg_traits<L1>::const_iterator
  1103. it = vect_const_begin(l1), ite = vect_const_end(l1);
  1104. for (size_type i = 0; it != ite; ++it, ++i)
  1105. if (*it != (typename linalg_traits<L1>::value_type)(0))
  1106. l2[i] = *it;
  1107. }
  1108. template <typename L1, typename L2>
  1109. void copy_vect(const L1& l1, L2& l2, abstract_skyline, abstract_sparse) {
  1110. clear(l2);
  1111. typename linalg_traits<L1>::const_iterator
  1112. it = vect_const_begin(l1), ite = vect_const_end(l1);
  1113. for (; it != ite; ++it)
  1114. if (*it != (typename linalg_traits<L1>::value_type)(0))
  1115. l2[it.index()] = *it;
  1116. }
  1117. /* ******************************************************************** */
  1118. /* Matrix and vector addition */
  1119. /* algorithms are built in order to avoid some conflicts whith */
  1120. /* repeated arguments or with overlapping part of a same object. */
  1121. /* In the latter case, conflicts are still possible. */
  1122. /* ******************************************************************** */
  1123. ///@endcond
  1124. /** Add two vectors or matrices
  1125. @param l1
  1126. @param l2 contains on output, l2+l1.
  1127. */
  1128. template <typename L1, typename L2> inline
  1129. void add(const L1& l1, L2& l2) {
  1130. add_spec(l1, l2, typename linalg_traits<L2>::linalg_type());
  1131. }
  1132. ///@cond
  1133. template <typename L1, typename L2> inline
  1134. void add(const L1& l1, const L2& l2) { add(l1, linalg_const_cast(l2)); }
  1135. template <typename L1, typename L2> inline
  1136. void add_spec(const L1& l1, L2& l2, abstract_vector) {
  1137. GMM_ASSERT2(vect_size(l1) == vect_size(l2), "dimensions mismatch");
  1138. add(l1, l2, typename linalg_traits<L1>::storage_type(),
  1139. typename linalg_traits<L2>::storage_type());
  1140. }
  1141. template <typename L1, typename L2> inline
  1142. void add_spec(const L1& l1, L2& l2, abstract_matrix) {
  1143. size_type m = mat_nrows(l1), n = mat_ncols(l1);
  1144. GMM_ASSERT2(m==mat_nrows(l2) && n==mat_ncols(l2), "dimensions mismatch");
  1145. add(l1, l2, typename linalg_traits<L1>::sub_orientation(),
  1146. typename linalg_traits<L2>::sub_orientation());
  1147. }
  1148. template <typename L1, typename L2>
  1149. void add(const L1& l1, L2& l2, row_major, row_major) {
  1150. typename linalg_traits<L1>::const_row_iterator it1 = mat_row_begin(l1),
  1151. ite = mat_row_end(l1);
  1152. typename linalg_traits<L2>::row_iterator it2 = mat_row_begin(l2);
  1153. for ( ; it1 != ite; ++it1, ++it2)
  1154. add(linalg_traits<L1>::row(it1), linalg_traits<L2>::row(it2));
  1155. }
  1156. template <typename L1, typename L2>
  1157. void add(const L1& l1, L2& l2, col_major, col_major) {
  1158. typename linalg_traits<L1>::const_col_iterator
  1159. it1 = mat_col_const_begin(l1),
  1160. ite = mat_col_const_end(l1);
  1161. typename linalg_traits<L2>::col_iterator it2 = mat_col_begin(l2);
  1162. for ( ; it1 != ite; ++it1, ++it2)
  1163. add(linalg_traits<L1>::col(it1), linalg_traits<L2>::col(it2));
  1164. }
  1165. template <typename L1, typename L2> inline
  1166. void add_mat_mixed_rc(const L1& l1, L2& l2, size_type i) {
  1167. add_mat_mixed_rc(l1, l2, i, typename linalg_traits<L1>::storage_type());
  1168. }
  1169. template <typename L1, typename L2>
  1170. void add_mat_mixed_rc(const L1& l1, L2& l2, size_type i, abstract_sparse) {
  1171. typename linalg_traits<L1>::const_iterator
  1172. it = vect_const_begin(l1), ite = vect_const_end(l1);
  1173. for (; it != ite; ++it) l2(i, it.index()) += *it;
  1174. }
  1175. template <typename L1, typename L2>
  1176. void add_mat_mixed_rc(const L1& l1, L2& l2, size_type i, abstract_skyline) {
  1177. typename linalg_traits<L1>::const_iterator
  1178. it = vect_const_begin(l1), ite = vect_const_end(l1);
  1179. for (; it != ite; ++it) l2(i, it.index()) += *it;
  1180. }
  1181. template <typename L1, typename L2>
  1182. void add_mat_mixed_rc(const L1& l1, L2& l2, size_type i, abstract_dense) {
  1183. typename linalg_traits<L1>::const_iterator
  1184. it = vect_const_begin(l1), ite = vect_const_end(l1);
  1185. for (size_type j = 0; it != ite; ++it, ++j) l2(i, j) += *it;
  1186. }
  1187. template <typename L1, typename L2> inline
  1188. void add_mat_mixed_cr(const L1& l1, L2& l2, size_type i) {
  1189. add_mat_mixed_cr(l1, l2, i, typename linalg_traits<L1>::storage_type());
  1190. }
  1191. template <typename L1, typename L2>
  1192. void add_mat_mixed_cr(const L1& l1, L2& l2, size_type i, abstract_sparse) {
  1193. typename linalg_traits<L1>::const_iterator
  1194. it = vect_const_begin(l1), ite = vect_const_end(l1);
  1195. for (; it != ite; ++it) l2(it.index(), i) += *it;
  1196. }
  1197. template <typename L1, typename L2>
  1198. void add_mat_mixed_cr(const L1& l1, L2& l2, size_type i, abstract_skyline) {
  1199. typename linalg_traits<L1>::const_iterator
  1200. it = vect_const_begin(l1), ite = vect_const_end(l1);
  1201. for (; it != ite; ++it) l2(it.index(), i) += *it;
  1202. }
  1203. template <typename L1, typename L2>
  1204. void add_mat_mixed_cr(const L1& l1, L2& l2, size_type i, abstract_dense) {
  1205. typename linalg_traits<L1>::const_iterator
  1206. it = vect_const_begin(l1), ite = vect_const_end(l1);
  1207. for (size_type j = 0; it != ite; ++it, ++j) l2(j, i) += *it;
  1208. }
  1209. template <typename L1, typename L2>
  1210. void add(const L1& l1, L2& l2, row_major, col_major) {
  1211. size_type nbr = mat_nrows(l1);
  1212. for (size_type i = 0; i < nbr; ++i)
  1213. add_mat_mixed_rc(mat_const_row(l1, i), l2, i);
  1214. }
  1215. template <typename L1, typename L2>
  1216. void add(const L1& l1, L2& l2, col_major, row_major) {
  1217. size_type nbc = mat_ncols(l1);
  1218. for (size_type i = 0; i < nbc; ++i)
  1219. add_mat_mixed_cr(mat_const_col(l1, i), l2, i);
  1220. }
  1221. template <typename L1, typename L2> inline
  1222. void add(const L1& l1, L2& l2, row_and_col, row_major)
  1223. { add(l1, l2, row_major(), row_major()); }
  1224. template <typename L1, typename L2> inline
  1225. void add(const L1& l1, L2& l2, row_and_col, row_and_col)
  1226. { add(l1, l2, row_major(), row_major()); }
  1227. template <typename L1, typename L2> inline
  1228. void add(const L1& l1, L2& l2, row_and_col, col_and_row)
  1229. { add(l1, l2, row_major(), row_major()); }
  1230. template <typename L1, typename L2> inline
  1231. void add(const L1& l1, L2& l2, col_and_row, row_and_col)
  1232. { add(l1, l2, row_major(), row_major()); }
  1233. template <typename L1, typename L2> inline
  1234. void add(const L1& l1, L2& l2, row_major, row_and_col)
  1235. { add(l1, l2, row_major(), row_major()); }
  1236. template <typename L1, typename L2> inline
  1237. void add(const L1& l1, L2& l2, col_and_row, row_major)
  1238. { add(l1, l2, row_major(), row_major()); }
  1239. template <typename L1, typename L2> inline
  1240. void add(const L1& l1, L2& l2, row_major, col_and_row)
  1241. { add(l1, l2, row_major(), row_major()); }
  1242. template <typename L1, typename L2> inline
  1243. void add(const L1& l1, L2& l2, row_and_col, col_major)
  1244. { add(l1, l2, col_major(), col_major()); }
  1245. template <typename L1, typename L2> inline
  1246. void add(const L1& l1, L2& l2, col_major, row_and_col)
  1247. { add(l1, l2, col_major(), col_major()); }
  1248. template <typename L1, typename L2> inline
  1249. void add(const L1& l1, L2& l2, col_and_row, col_major)
  1250. { add(l1, l2, col_major(), col_major()); }
  1251. template <typename L1, typename L2> inline
  1252. void add(const L1& l1, L2& l2, col_and_row, col_and_row)
  1253. { add(l1, l2, col_major(), col_major()); }
  1254. template <typename L1, typename L2> inline
  1255. void add(const L1& l1, L2& l2, col_major, col_and_row)
  1256. { add(l1, l2, col_major(), col_major()); }
  1257. ///@endcond
  1258. /** Addition of two vectors/matrices
  1259. @param l1
  1260. @param l2
  1261. @param l3 contains l1+l2 on output
  1262. */
  1263. template <typename L1, typename L2, typename L3> inline
  1264. void add(const L1& l1, const L2& l2, L3& l3) {
  1265. add_spec(l1, l2, l3, typename linalg_traits<L2>::linalg_type());
  1266. }
  1267. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  1268. template <typename L1, typename L2, typename L3> inline
  1269. void add(const L1& l1, const L2& l2, const L3& l3)
  1270. { add(l1, l2, linalg_const_cast(l3)); }
  1271. template <typename L1, typename L2, typename L3> inline
  1272. void add_spec(const L1& l1, const L2& l2, L3& l3, abstract_matrix)
  1273. { copy(l2, l3); add(l1, l3); }
  1274. template <typename L1, typename L2, typename L3> inline
  1275. void add_spec(const L1& l1, const L2& l2, L3& l3, abstract_vector) {
  1276. GMM_ASSERT2(vect_size(l1) == vect_size(l2) &&
  1277. vect_size(l1) == vect_size(l3), "dimensions mismatch");
  1278. if ((const void *)(&l1) == (const void *)(&l3))
  1279. add(l2, l3);
  1280. else if ((const void *)(&l2) == (const void *)(&l3))
  1281. add(l1, l3);
  1282. else
  1283. add(l1, l2, l3, typename linalg_traits<L1>::storage_type(),
  1284. typename linalg_traits<L2>::storage_type(),
  1285. typename linalg_traits<L3>::storage_type());
  1286. }
  1287. template <typename IT1, typename IT2, typename IT3>
  1288. void add_full_(IT1 it1, IT2 it2, IT3 it3, IT3 ite) {
  1289. for (; it3 != ite; ++it3, ++it2, ++it1) *it3 = *it1 + *it2;
  1290. }
  1291. template <typename IT1, typename IT2, typename IT3>
  1292. void add_almost_full_(IT1 it1, IT1 ite1, IT2 it2, IT3 it3, IT3 ite3) {
  1293. IT3 it = it3;
  1294. for (; it != ite3; ++it, ++it2) *it = *it2;
  1295. for (; it1 != ite1; ++it1)
  1296. *(it3 + it1.index()) += *it1;
  1297. }
  1298. template <typename IT1, typename IT2, typename IT3>
  1299. void add_to_full_(IT1 it1, IT1 ite1, IT2 it2, IT2 ite2,
  1300. IT3 it3, IT3 ite3) {
  1301. typedef typename std::iterator_traits<IT3>::value_type T;
  1302. IT3 it = it3;
  1303. for (; it != ite3; ++it) *it = T(0);
  1304. for (; it1 != ite1; ++it1) *(it3 + it1.index()) = *it1;
  1305. for (; it2 != ite2; ++it2) *(it3 + it2.index()) += *it2;
  1306. }
  1307. template <typename L1, typename L2, typename L3> inline
  1308. void add(const L1& l1, const L2& l2, L3& l3,
  1309. abstract_dense, abstract_dense, abstract_dense) {
  1310. add_full_(vect_const_begin(l1), vect_const_begin(l2),
  1311. vect_begin(l3), vect_end(l3));
  1312. }
  1313. // generic function for add(v1, v2, v3).
  1314. // Need to be specialized to optimize particular additions.
  1315. template <typename L1, typename L2, typename L3,
  1316. typename ST1, typename ST2, typename ST3>
  1317. inline void add(const L1& l1, const L2& l2, L3& l3, ST1, ST2, ST3)
  1318. { copy(l2, l3); add(l1, l3, ST1(), ST3()); }
  1319. template <typename L1, typename L2, typename L3> inline
  1320. void add(const L1& l1, const L2& l2, L3& l3,
  1321. abstract_sparse, abstract_dense, abstract_dense) {
  1322. add_almost_full_(vect_const_begin(l1), vect_const_end(l1),
  1323. vect_const_begin(l2), vect_begin(l3), vect_end(l3));
  1324. }
  1325. template <typename L1, typename L2, typename L3> inline
  1326. void add(const L1& l1, const L2& l2, L3& l3,
  1327. abstract_dense, abstract_sparse, abstract_dense)
  1328. { add(l2, l1, l3, abstract_sparse(), abstract_dense(), abstract_dense()); }
  1329. template <typename L1, typename L2, typename L3> inline
  1330. void add(const L1& l1, const L2& l2, L3& l3,
  1331. abstract_sparse, abstract_sparse, abstract_dense) {
  1332. add_to_full_(vect_const_begin(l1), vect_const_end(l1),
  1333. vect_const_begin(l2), vect_const_end(l2),
  1334. vect_begin(l3), vect_end(l3));
  1335. }
  1336. template <typename L1, typename L2, typename L3>
  1337. void add_spspsp(const L1& l1, const L2& l2, L3& l3, linalg_true) {
  1338. typename linalg_traits<L1>::const_iterator
  1339. it1 = vect_const_begin(l1), ite1 = vect_const_end(l1);
  1340. typename linalg_traits<L2>::const_iterator
  1341. it2 = vect_const_begin(l2), ite2 = vect_const_end(l2);
  1342. clear(l3);
  1343. while (it1 != ite1 && it2 != ite2) {
  1344. ptrdiff_t d = it1.index() - it2.index();
  1345. if (d < 0)
  1346. { l3[it1.index()] += *it1; ++it1; }
  1347. else if (d > 0)
  1348. { l3[it2.index()] += *it2; ++it2; }
  1349. else
  1350. { l3[it1.index()] = *it1 + *it2; ++it1; ++it2; }
  1351. }
  1352. for (; it1 != ite1; ++it1) l3[it1.index()] += *it1;
  1353. for (; it2 != ite2; ++it2) l3[it2.index()] += *it2;
  1354. }
  1355. template <typename L1, typename L2, typename L3>
  1356. void add_spspsp(const L1& l1, const L2& l2, L3& l3, linalg_false)
  1357. { copy(l2, l3); add(l2, l3); }
  1358. template <typename L1, typename L2, typename L3>
  1359. void add(const L1& l1, const L2& l2, L3& l3,
  1360. abstract_sparse, abstract_sparse, abstract_sparse) {
  1361. add_spspsp(l1, l2, l3, typename linalg_and<typename
  1362. linalg_traits<L1>::index_sorted,
  1363. typename linalg_traits<L2>::index_sorted>::bool_type());
  1364. }
  1365. template <typename L1, typename L2>
  1366. void add(const L1& l1, L2& l2, abstract_dense, abstract_dense) {
  1367. typename linalg_traits<L1>::const_iterator it1 = vect_const_begin(l1);
  1368. typename linalg_traits<L2>::iterator
  1369. it2 = vect_begin(l2), ite = vect_end(l2);
  1370. for (; it2 != ite; ++it2, ++it1) *it2 += *it1;
  1371. }
  1372. template <typename L1, typename L2>
  1373. void add(const L1& l1, L2& l2, abstract_dense, abstract_skyline) {
  1374. typedef typename linalg_traits<L1>::const_iterator const_l1_iterator;
  1375. typedef typename linalg_traits<L2>::iterator l2_iterator;
  1376. typedef typename linalg_traits<L1>::value_type T;
  1377. const_l1_iterator it1 = vect_const_begin(l1), ite1 = vect_const_end(l1);
  1378. size_type i1 = 0, ie1 = vect_size(l1);
  1379. while (it1 != ite1 && *it1 == T(0)) { ++it1; ++i1; }
  1380. if (it1 != ite1) {
  1381. l2_iterator it2 = vect_begin(l2), ite2 = vect_end(l2);
  1382. while (ie1 && *(ite1-1) == T(0)) { ite1--; --ie1; }
  1383. if (it2 == ite2 || i1 < it2.index()) {
  1384. l2[i1] = *it1; ++i1; ++it1;
  1385. if (it1 == ite1) return;
  1386. it2 = vect_begin(l2); ite2 = vect_end(l2);
  1387. }
  1388. if (ie1 > ite2.index()) {
  1389. --ite1; l2[ie1 - 1] = *ite1;
  1390. it2 = vect_begin(l2);
  1391. }
  1392. it2 += i1 - it2.index();
  1393. for (; it1 != ite1; ++it1, ++it2) { *it2 += *it1; }
  1394. }
  1395. }
  1396. template <typename L1, typename L2>
  1397. void add(const L1& l1, L2& l2, abstract_skyline, abstract_dense) {
  1398. typename linalg_traits<L1>::const_iterator it1 = vect_const_begin(l1),
  1399. ite1 = vect_const_end(l1);
  1400. if (it1 != ite1) {
  1401. typename linalg_traits<L2>::iterator it2 = vect_begin(l2);
  1402. it2 += it1.index();
  1403. for (; it1 != ite1; ++it2, ++it1) *it2 += *it1;
  1404. }
  1405. }
  1406. template <typename L1, typename L2>
  1407. void add(const L1& l1, L2& l2, abstract_sparse, abstract_dense) {
  1408. typename linalg_traits<L1>::const_iterator
  1409. it1 = vect_const_begin(l1), ite1 = vect_const_end(l1);
  1410. for (; it1 != ite1; ++it1) l2[it1.index()] += *it1;
  1411. }
  1412. template <typename L1, typename L2>
  1413. void add(const L1& l1, L2& l2, abstract_sparse, abstract_sparse) {
  1414. typename linalg_traits<L1>::const_iterator
  1415. it1 = vect_const_begin(l1), ite1 = vect_const_end(l1);
  1416. for (; it1 != ite1; ++it1) l2[it1.index()] += *it1;
  1417. }
  1418. template <typename L1, typename L2>
  1419. void add(const L1& l1, L2& l2, abstract_sparse, abstract_skyline) {
  1420. typename linalg_traits<L1>::const_iterator
  1421. it1 = vect_const_begin(l1), ite1 = vect_const_end(l1);
  1422. for (; it1 != ite1; ++it1) l2[it1.index()] += *it1;
  1423. }
  1424. template <typename L1, typename L2>
  1425. void add(const L1& l1, L2& l2, abstract_skyline, abstract_sparse) {
  1426. typename linalg_traits<L1>::const_iterator
  1427. it1 = vect_const_begin(l1), ite1 = vect_const_end(l1);
  1428. for (; it1 != ite1; ++it1)
  1429. if (*it1 != typename linalg_traits<L1>::value_type(0))
  1430. l2[it1.index()] += *it1;
  1431. }
  1432. template <typename L1, typename L2>
  1433. void add(const L1& l1, L2& l2, abstract_skyline, abstract_skyline) {
  1434. typedef typename linalg_traits<L1>::const_iterator const_l1_iterator;
  1435. typedef typename linalg_traits<L2>::iterator l2_iterator;
  1436. typedef typename linalg_traits<L1>::value_type T1;
  1437. typedef typename linalg_traits<L2>::value_type T2;
  1438. const_l1_iterator it1 = vect_const_begin(l1), ite1 = vect_const_end(l1);
  1439. while (it1 != ite1 && *it1 == T1(0)) ++it1;
  1440. if (ite1 != it1) {
  1441. l2_iterator it2 = vect_begin(l2), ite2 = vect_end(l2);
  1442. while (*(ite1-1) == T1(0)) ite1--;
  1443. if (it2 == ite2 || it1.index() < it2.index()) {
  1444. l2[it1.index()] = T2(0);
  1445. it2 = vect_begin(l2); ite2 = vect_end(l2);
  1446. }
  1447. if (ite1.index() > ite2.index()) {
  1448. l2[ite1.index() - 1] = T2(0);
  1449. it2 = vect_begin(l2);
  1450. }
  1451. it2 += it1.index() - it2.index();
  1452. for (; it1 != ite1; ++it1, ++it2) *it2 += *it1;
  1453. }
  1454. }
  1455. template <typename L1, typename L2>
  1456. void add(const L1& l1, L2& l2, abstract_dense, abstract_sparse) {
  1457. typename linalg_traits<L1>::const_iterator
  1458. it1 = vect_const_begin(l1), ite1 = vect_const_end(l1);
  1459. for (size_type i = 0; it1 != ite1; ++it1, ++i)
  1460. if (*it1 != typename linalg_traits<L1>::value_type(0)) l2[i] += *it1;
  1461. }
  1462. /* ******************************************************************** */
  1463. /* Matrix-vector mult */
  1464. /* ******************************************************************** */
  1465. ///@endcond
  1466. /** matrix-vector or matrix-matrix product.
  1467. @param l1 a matrix.
  1468. @param l2 a vector or matrix.
  1469. @param l3 the product l1*l2.
  1470. */
  1471. template <typename L1, typename L2, typename L3> inline
  1472. void mult(const L1& l1, const L2& l2, L3& l3) {
  1473. mult_dispatch(l1, l2, l3, typename linalg_traits<L2>::linalg_type());
  1474. }
  1475. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  1476. template <typename L1, typename L2, typename L3> inline
  1477. void mult(const L1& l1, const L2& l2, const L3& l3)
  1478. { mult(l1, l2, linalg_const_cast(l3)); }
  1479. template <typename L1, typename L2, typename L3> inline
  1480. void mult_dispatch(const L1& l1, const L2& l2, L3& l3, abstract_vector) {
  1481. size_type m = mat_nrows(l1), n = mat_ncols(l1);
  1482. if (!m || !n) { gmm::clear(l3); return; }
  1483. GMM_ASSERT2(n==vect_size(l2) && m==vect_size(l3), "dimensions mismatch");
  1484. if (!same_origin(l2, l3))
  1485. mult_spec(l1, l2, l3, typename principal_orientation_type<typename
  1486. linalg_traits<L1>::sub_orientation>::potype());
  1487. else {
  1488. GMM_WARNING2("Warning, A temporary is used for mult\n");
  1489. typename temporary_vector<L3>::vector_type temp(vect_size(l3));
  1490. mult_spec(l1, l2, temp, typename principal_orientation_type<typename
  1491. linalg_traits<L1>::sub_orientation>::potype());
  1492. copy(temp, l3);
  1493. }
  1494. }
  1495. template <typename L1, typename L2, typename L3>
  1496. void mult_by_row(const L1& l1, const L2& l2, L3& l3, abstract_sparse) {
  1497. typedef typename linalg_traits<L3>::value_type T;
  1498. clear(l3);
  1499. size_type nr = mat_nrows(l1);
  1500. for (size_type i = 0; i < nr; ++i) {
  1501. T aux = vect_sp(mat_const_row(l1, i), l2);
  1502. if (aux != T(0)) l3[i] = aux;
  1503. }
  1504. }
  1505. template <typename L1, typename L2, typename L3>
  1506. void mult_by_row(const L1& l1, const L2& l2, L3& l3, abstract_skyline) {
  1507. typedef typename linalg_traits<L3>::value_type T;
  1508. clear(l3);
  1509. size_type nr = mat_nrows(l1);
  1510. for (size_type i = 0; i < nr; ++i) {
  1511. T aux = vect_sp(mat_const_row(l1, i), l2);
  1512. if (aux != T(0)) l3[i] = aux;
  1513. }
  1514. }
  1515. #ifdef STORM_HAVE_INTELTBB
  1516. /* Official Intel Hint on blocked_range vs. linear iterators: http://software.intel.com/en-us/forums/topic/289505
  1517. */
  1518. template <typename IT1, typename IT2>
  1519. class forward_range_mult {
  1520. IT1 my_begin;
  1521. IT1 my_end;
  1522. IT2 my_begin_row;
  1523. size_t my_size;
  1524. public:
  1525. IT1 begin() const {return my_begin;}
  1526. IT2 begin_row() const {return my_begin_row;}
  1527. IT1 end() const {return my_end;}
  1528. bool empty() const {return my_begin==my_end;}
  1529. bool is_divisible() const {return my_size>1;}
  1530. forward_range_mult( IT1 first, IT1 last, IT2 row_first, size_t size ) : my_begin(first), my_end(last), my_begin_row(row_first), my_size(size) {
  1531. assert( size==size_t(std::distance( first,last )));
  1532. }
  1533. forward_range_mult( IT1 first, IT1 last, IT2 row_first) : my_begin(first), my_end(last), my_begin_row(row_first) {
  1534. my_size = std::distance( first,last );
  1535. }
  1536. forward_range_mult( forward_range_mult& r, tbb::split ) {
  1537. size_t h = r.my_size/2;
  1538. my_end = r.my_end;
  1539. my_begin = r.my_begin;
  1540. my_begin_row = r.my_begin_row;
  1541. std::advance( my_begin, h ); // Might be scaling issue
  1542. std::advance( my_begin_row, h );
  1543. my_size = r.my_size-h;
  1544. r.my_end = my_begin;
  1545. r.my_size = h;
  1546. }
  1547. };
  1548. template <typename L1, typename L2, typename L3>
  1549. class tbbHelper_mult_by_row {
  1550. L2 const* my_l2;
  1551. // Typedefs for Iterator Types
  1552. typedef typename linalg_traits<L3>::iterator frm_IT1;
  1553. typedef typename linalg_traits<L1>::const_row_iterator frm_IT2;
  1554. public:
  1555. void operator()( const forward_range_mult<frm_IT1, frm_IT2>& r ) const {
  1556. L2 const& l2 = *my_l2;
  1557. frm_IT1 it = r.begin();
  1558. frm_IT1 ite = r.end();
  1559. frm_IT2 itr = r.begin_row();
  1560. for (; it != ite; ++it, ++itr) {
  1561. *it = vect_sp(linalg_traits<L1>::row(itr), l2,
  1562. typename linalg_traits<L1>::storage_type(),
  1563. typename linalg_traits<L2>::storage_type());
  1564. }
  1565. }
  1566. tbbHelper_mult_by_row(L2 const* l2) :
  1567. my_l2(l2)
  1568. {}
  1569. };
  1570. #endif
  1571. template <typename L1, typename L2, typename L3>
  1572. void mult_by_row(const L1& l1, const L2& l2, L3& l3, abstract_dense) {
  1573. typename linalg_traits<L3>::iterator it=vect_begin(l3), ite=vect_end(l3);
  1574. typename linalg_traits<L1>::const_row_iterator
  1575. itr = mat_row_const_begin(l1);
  1576. #ifdef STORM_HAVE_INTELTBB
  1577. tbb::parallel_for(forward_range_mult<typename linalg_traits<L3>::iterator, typename linalg_traits<L1>::const_row_iterator>(it, ite, itr), tbbHelper_mult_by_row<L1, L2, L3>(&l2));
  1578. #else
  1579. for (; it != ite; ++it, ++itr)
  1580. *it = vect_sp(linalg_traits<L1>::row(itr), l2,
  1581. typename linalg_traits<L1>::storage_type(),
  1582. typename linalg_traits<L2>::storage_type());
  1583. #endif
  1584. }
  1585. template <typename L1, typename L2, typename L3>
  1586. void mult_by_col(const L1& l1, const L2& l2, L3& l3, abstract_dense) {
  1587. clear(l3);
  1588. size_type nc = mat_ncols(l1);
  1589. for (size_type i = 0; i < nc; ++i)
  1590. add(scaled(mat_const_col(l1, i), l2[i]), l3);
  1591. }
  1592. template <typename L1, typename L2, typename L3>
  1593. void mult_by_col(const L1& l1, const L2& l2, L3& l3, abstract_sparse) {
  1594. typedef typename linalg_traits<L2>::value_type T;
  1595. clear(l3);
  1596. typename linalg_traits<L2>::const_iterator it = vect_const_begin(l2),
  1597. ite = vect_const_end(l2);
  1598. for (; it != ite; ++it)
  1599. if (*it != T(0)) add(scaled(mat_const_col(l1, it.index()), *it), l3);
  1600. }
  1601. template <typename L1, typename L2, typename L3>
  1602. void mult_by_col(const L1& l1, const L2& l2, L3& l3, abstract_skyline) {
  1603. typedef typename linalg_traits<L2>::value_type T;
  1604. clear(l3);
  1605. typename linalg_traits<L2>::const_iterator it = vect_const_begin(l2),
  1606. ite = vect_const_end(l2);
  1607. for (; it != ite; ++it)
  1608. if (*it != T(0)) add(scaled(mat_const_col(l1, it.index()), *it), l3);
  1609. }
  1610. template <typename L1, typename L2, typename L3> inline
  1611. void mult_spec(const L1& l1, const L2& l2, L3& l3, row_major)
  1612. { mult_by_row(l1, l2, l3, typename linalg_traits<L3>::storage_type()); }
  1613. template <typename L1, typename L2, typename L3> inline
  1614. void mult_spec(const L1& l1, const L2& l2, L3& l3, col_major)
  1615. { mult_by_col(l1, l2, l3, typename linalg_traits<L2>::storage_type()); }
  1616. template <typename L1, typename L2, typename L3> inline
  1617. void mult_spec(const L1& l1, const L2& l2, L3& l3, abstract_null_type)
  1618. { mult_ind(l1, l2, l3, typename linalg_traits<L1>::storage_type()); }
  1619. template <typename L1, typename L2, typename L3>
  1620. void mult_ind(const L1& l1, const L2& l2, L3& l3, abstract_indirect) {
  1621. GMM_ASSERT1(false, "gmm::mult(m, ., .) undefined for this kind of matrix");
  1622. }
  1623. template <typename L1, typename L2, typename L3, typename L4> inline
  1624. void mult(const L1& l1, const L2& l2, const L3& l3, L4& l4) {
  1625. size_type m = mat_nrows(l1), n = mat_ncols(l1);
  1626. copy(l3, l4);
  1627. if (!m || !n) { gmm::copy(l3, l4); return; }
  1628. GMM_ASSERT2(n==vect_size(l2) && m==vect_size(l4), "dimensions mismatch");
  1629. if (!same_origin(l2, l4)) {
  1630. mult_add_spec(l1, l2, l4, typename principal_orientation_type<typename
  1631. linalg_traits<L1>::sub_orientation>::potype());
  1632. }
  1633. else {
  1634. GMM_WARNING2("Warning, A temporary is used for mult\n");
  1635. typename temporary_vector<L2>::vector_type temp(vect_size(l2));
  1636. copy(l2, temp);
  1637. mult_add_spec(l1,temp, l4, typename principal_orientation_type<typename
  1638. linalg_traits<L1>::sub_orientation>::potype());
  1639. }
  1640. }
  1641. template <typename L1, typename L2, typename L3, typename L4> inline
  1642. void mult(const L1& l1, const L2& l2, const L3& l3, const L4& l4)
  1643. { mult(l1, l2, l3, linalg_const_cast(l4)); }
  1644. ///@endcond
  1645. /** Multiply-accumulate. l3 += l1*l2; */
  1646. template <typename L1, typename L2, typename L3> inline
  1647. void mult_add(const L1& l1, const L2& l2, L3& l3) {
  1648. size_type m = mat_nrows(l1), n = mat_ncols(l1);
  1649. if (!m || !n) return;
  1650. GMM_ASSERT2(n==vect_size(l2) && m==vect_size(l3), "dimensions mismatch");
  1651. if (!same_origin(l2, l3)) {
  1652. mult_add_spec(l1, l2, l3, typename principal_orientation_type<typename
  1653. linalg_traits<L1>::sub_orientation>::potype());
  1654. }
  1655. else {
  1656. GMM_WARNING2("Warning, A temporary is used for mult\n");
  1657. typename temporary_vector<L3>::vector_type temp(vect_size(l2));
  1658. copy(l2, temp);
  1659. mult_add_spec(l1,temp, l3, typename principal_orientation_type<typename
  1660. linalg_traits<L1>::sub_orientation>::potype());
  1661. }
  1662. }
  1663. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  1664. template <typename L1, typename L2, typename L3> inline
  1665. void mult_add(const L1& l1, const L2& l2, const L3& l3)
  1666. { mult_add(l1, l2, linalg_const_cast(l3)); }
  1667. template <typename L1, typename L2, typename L3>
  1668. void mult_add_by_row(const L1& l1, const L2& l2, L3& l3, abstract_sparse) {
  1669. typedef typename linalg_traits<L3>::value_type T;
  1670. size_type nr = mat_nrows(l1);
  1671. for (size_type i = 0; i < nr; ++i) {
  1672. T aux = vect_sp(mat_const_row(l1, i), l2);
  1673. if (aux != T(0)) l3[i] += aux;
  1674. }
  1675. }
  1676. template <typename L1, typename L2, typename L3>
  1677. void mult_add_by_row(const L1& l1, const L2& l2, L3& l3, abstract_skyline) {
  1678. typedef typename linalg_traits<L3>::value_type T;
  1679. size_type nr = mat_nrows(l1);
  1680. for (size_type i = 0; i < nr; ++i) {
  1681. T aux = vect_sp(mat_const_row(l1, i), l2);
  1682. if (aux != T(0)) l3[i] += aux;
  1683. }
  1684. }
  1685. template <typename L1, typename L2, typename L3>
  1686. void mult_add_by_row(const L1& l1, const L2& l2, L3& l3, abstract_dense) {
  1687. typename linalg_traits<L3>::iterator it=vect_begin(l3), ite=vect_end(l3);
  1688. typename linalg_traits<L1>::const_row_iterator
  1689. itr = mat_row_const_begin(l1);
  1690. for (; it != ite; ++it, ++itr)
  1691. *it += vect_sp(linalg_traits<L1>::row(itr), l2);
  1692. }
  1693. template <typename L1, typename L2, typename L3>
  1694. void mult_add_by_col(const L1& l1, const L2& l2, L3& l3, abstract_dense) {
  1695. size_type nc = mat_ncols(l1);
  1696. for (size_type i = 0; i < nc; ++i)
  1697. add(scaled(mat_const_col(l1, i), l2[i]), l3);
  1698. }
  1699. template <typename L1, typename L2, typename L3>
  1700. void mult_add_by_col(const L1& l1, const L2& l2, L3& l3, abstract_sparse) {
  1701. typename linalg_traits<L2>::const_iterator it = vect_const_begin(l2),
  1702. ite = vect_const_end(l2);
  1703. for (; it != ite; ++it)
  1704. if (*it != typename linalg_traits<L2>::value_type(0))
  1705. add(scaled(mat_const_col(l1, it.index()), *it), l3);
  1706. }
  1707. template <typename L1, typename L2, typename L3>
  1708. void mult_add_by_col(const L1& l1, const L2& l2, L3& l3, abstract_skyline) {
  1709. typename linalg_traits<L2>::const_iterator it = vect_const_begin(l2),
  1710. ite = vect_const_end(l2);
  1711. for (; it != ite; ++it)
  1712. if (*it != typename linalg_traits<L2>::value_type(0))
  1713. add(scaled(mat_const_col(l1, it.index()), *it), l3);
  1714. }
  1715. template <typename L1, typename L2, typename L3> inline
  1716. void mult_add_spec(const L1& l1, const L2& l2, L3& l3, row_major)
  1717. { mult_add_by_row(l1, l2, l3, typename linalg_traits<L3>::storage_type()); }
  1718. template <typename L1, typename L2, typename L3> inline
  1719. void mult_add_spec(const L1& l1, const L2& l2, L3& l3, col_major)
  1720. { mult_add_by_col(l1, l2, l3, typename linalg_traits<L2>::storage_type()); }
  1721. template <typename L1, typename L2, typename L3> inline
  1722. void mult_add_spec(const L1& l1, const L2& l2, L3& l3, abstract_null_type)
  1723. { mult_ind(l1, l2, l3, typename linalg_traits<L1>::storage_type()); }
  1724. template <typename L1, typename L2, typename L3>
  1725. void transposed_mult(const L1& l1, const L2& l2, const L3& l3)
  1726. { mult(gmm::transposed(l1), l2, l3); }
  1727. /* ******************************************************************** */
  1728. /* Matrix-matrix mult */
  1729. /* ******************************************************************** */
  1730. struct g_mult {}; // generic mult, less optimized
  1731. struct c_mult {}; // col x col -> col mult
  1732. struct r_mult {}; // row x row -> row mult
  1733. struct rcmult {}; // row x col -> col mult
  1734. struct crmult {}; // col x row -> row mult
  1735. template<typename SO1, typename SO2, typename SO3> struct mult_t;
  1736. #define DEFMU__ template<> struct mult_t
  1737. DEFMU__<row_major , row_major , row_major > { typedef r_mult t; };
  1738. DEFMU__<row_major , row_major , col_major > { typedef g_mult t; };
  1739. DEFMU__<row_major , row_major , col_and_row> { typedef r_mult t; };
  1740. DEFMU__<row_major , row_major , row_and_col> { typedef r_mult t; };
  1741. DEFMU__<row_major , col_major , row_major > { typedef rcmult t; };
  1742. DEFMU__<row_major , col_major , col_major > { typedef rcmult t; };
  1743. DEFMU__<row_major , col_major , col_and_row> { typedef rcmult t; };
  1744. DEFMU__<row_major , col_major , row_and_col> { typedef rcmult t; };
  1745. DEFMU__<row_major , col_and_row, row_major > { typedef r_mult t; };
  1746. DEFMU__<row_major , col_and_row, col_major > { typedef rcmult t; };
  1747. DEFMU__<row_major , col_and_row, col_and_row> { typedef rcmult t; };
  1748. DEFMU__<row_major , col_and_row, row_and_col> { typedef rcmult t; };
  1749. DEFMU__<row_major , row_and_col, row_major > { typedef r_mult t; };
  1750. DEFMU__<row_major , row_and_col, col_major > { typedef rcmult t; };
  1751. DEFMU__<row_major , row_and_col, col_and_row> { typedef r_mult t; };
  1752. DEFMU__<row_major , row_and_col, row_and_col> { typedef r_mult t; };
  1753. DEFMU__<col_major , row_major , row_major > { typedef crmult t; };
  1754. DEFMU__<col_major , row_major , col_major > { typedef g_mult t; };
  1755. DEFMU__<col_major , row_major , col_and_row> { typedef crmult t; };
  1756. DEFMU__<col_major , row_major , row_and_col> { typedef crmult t; };
  1757. DEFMU__<col_major , col_major , row_major > { typedef g_mult t; };
  1758. DEFMU__<col_major , col_major , col_major > { typedef c_mult t; };
  1759. DEFMU__<col_major , col_major , col_and_row> { typedef c_mult t; };
  1760. DEFMU__<col_major , col_major , row_and_col> { typedef c_mult t; };
  1761. DEFMU__<col_major , col_and_row, row_major > { typedef crmult t; };
  1762. DEFMU__<col_major , col_and_row, col_major > { typedef c_mult t; };
  1763. DEFMU__<col_major , col_and_row, col_and_row> { typedef c_mult t; };
  1764. DEFMU__<col_major , col_and_row, row_and_col> { typedef c_mult t; };
  1765. DEFMU__<col_major , row_and_col, row_major > { typedef crmult t; };
  1766. DEFMU__<col_major , row_and_col, col_major > { typedef c_mult t; };
  1767. DEFMU__<col_major , row_and_col, col_and_row> { typedef c_mult t; };
  1768. DEFMU__<col_major , row_and_col, row_and_col> { typedef c_mult t; };
  1769. DEFMU__<col_and_row, row_major , row_major > { typedef r_mult t; };
  1770. DEFMU__<col_and_row, row_major , col_major > { typedef c_mult t; };
  1771. DEFMU__<col_and_row, row_major , col_and_row> { typedef r_mult t; };
  1772. DEFMU__<col_and_row, row_major , row_and_col> { typedef r_mult t; };
  1773. DEFMU__<col_and_row, col_major , row_major > { typedef rcmult t; };
  1774. DEFMU__<col_and_row, col_major , col_major > { typedef c_mult t; };
  1775. DEFMU__<col_and_row, col_major , col_and_row> { typedef c_mult t; };
  1776. DEFMU__<col_and_row, col_major , row_and_col> { typedef c_mult t; };
  1777. DEFMU__<col_and_row, col_and_row, row_major > { typedef r_mult t; };
  1778. DEFMU__<col_and_row, col_and_row, col_major > { typedef c_mult t; };
  1779. DEFMU__<col_and_row, col_and_row, col_and_row> { typedef c_mult t; };
  1780. DEFMU__<col_and_row, col_and_row, row_and_col> { typedef c_mult t; };
  1781. DEFMU__<col_and_row, row_and_col, row_major > { typedef r_mult t; };
  1782. DEFMU__<col_and_row, row_and_col, col_major > { typedef c_mult t; };
  1783. DEFMU__<col_and_row, row_and_col, col_and_row> { typedef c_mult t; };
  1784. DEFMU__<col_and_row, row_and_col, row_and_col> { typedef r_mult t; };
  1785. DEFMU__<row_and_col, row_major , row_major > { typedef r_mult t; };
  1786. DEFMU__<row_and_col, row_major , col_major > { typedef c_mult t; };
  1787. DEFMU__<row_and_col, row_major , col_and_row> { typedef r_mult t; };
  1788. DEFMU__<row_and_col, row_major , row_and_col> { typedef r_mult t; };
  1789. DEFMU__<row_and_col, col_major , row_major > { typedef rcmult t; };
  1790. DEFMU__<row_and_col, col_major , col_major > { typedef c_mult t; };
  1791. DEFMU__<row_and_col, col_major , col_and_row> { typedef c_mult t; };
  1792. DEFMU__<row_and_col, col_major , row_and_col> { typedef c_mult t; };
  1793. DEFMU__<row_and_col, col_and_row, row_major > { typedef rcmult t; };
  1794. DEFMU__<row_and_col, col_and_row, col_major > { typedef rcmult t; };
  1795. DEFMU__<row_and_col, col_and_row, col_and_row> { typedef rcmult t; };
  1796. DEFMU__<row_and_col, col_and_row, row_and_col> { typedef rcmult t; };
  1797. DEFMU__<row_and_col, row_and_col, row_major > { typedef r_mult t; };
  1798. DEFMU__<row_and_col, row_and_col, col_major > { typedef c_mult t; };
  1799. DEFMU__<row_and_col, row_and_col, col_and_row> { typedef r_mult t; };
  1800. DEFMU__<row_and_col, row_and_col, row_and_col> { typedef r_mult t; };
  1801. template <typename L1, typename L2, typename L3>
  1802. void mult_dispatch(const L1& l1, const L2& l2, L3& l3, abstract_matrix) {
  1803. typedef typename temporary_matrix<L3>::matrix_type temp_mat_type;
  1804. size_type m = mat_nrows(l1), n = mat_ncols(l1), k = mat_ncols(l2);
  1805. if (n == 0) { gmm::clear(l3); return; }
  1806. GMM_ASSERT2(n == mat_nrows(l2) && m == mat_nrows(l3) && k == mat_ncols(l3),
  1807. "dimensions mismatch");
  1808. if (same_origin(l2, l3) || same_origin(l1, l3)) {
  1809. GMM_WARNING2("A temporary is used for mult");
  1810. temp_mat_type temp(mat_nrows(l3), mat_ncols(l3));
  1811. mult_spec(l1, l2, temp, typename mult_t<
  1812. typename linalg_traits<L1>::sub_orientation,
  1813. typename linalg_traits<L2>::sub_orientation,
  1814. typename linalg_traits<temp_mat_type>::sub_orientation>::t());
  1815. copy(temp, l3);
  1816. }
  1817. else
  1818. mult_spec(l1, l2, l3, typename mult_t<
  1819. typename linalg_traits<L1>::sub_orientation,
  1820. typename linalg_traits<L2>::sub_orientation,
  1821. typename linalg_traits<L3>::sub_orientation>::t());
  1822. }
  1823. // Completely generic but inefficient
  1824. template <typename L1, typename L2, typename L3>
  1825. void mult_spec(const L1& l1, const L2& l2, L3& l3, g_mult) {
  1826. typedef typename linalg_traits<L3>::value_type T;
  1827. GMM_WARNING2("Inefficient generic matrix-matrix mult is used");
  1828. for (size_type i = 0; i < mat_nrows(l3) ; ++i)
  1829. for (size_type j = 0; j < mat_ncols(l3) ; ++j) {
  1830. T a(0);
  1831. for (size_type k = 0; k < mat_nrows(l2) ; ++k) a += l1(i, k)*l2(k, j);
  1832. l3(i, j) = a;
  1833. }
  1834. }
  1835. // row x col matrix-matrix mult
  1836. template <typename L1, typename L2, typename L3>
  1837. void mult_row_col_with_temp(const L1& l1, const L2& l2, L3& l3, col_major) {
  1838. typedef typename temporary_col_matrix<L1>::matrix_type temp_col_mat;
  1839. temp_col_mat temp(mat_nrows(l1), mat_ncols(l1));
  1840. copy(l1, temp);
  1841. mult(temp, l2, l3);
  1842. }
  1843. template <typename L1, typename L2, typename L3>
  1844. void mult_row_col_with_temp(const L1& l1, const L2& l2, L3& l3, row_major) {
  1845. typedef typename temporary_row_matrix<L2>::matrix_type temp_row_mat;
  1846. temp_row_mat temp(mat_nrows(l2), mat_ncols(l2));
  1847. copy(l2, temp);
  1848. mult(l1, temp, l3);
  1849. }
  1850. template <typename L1, typename L2, typename L3>
  1851. void mult_spec(const L1& l1, const L2& l2, L3& l3, rcmult) {
  1852. if (is_sparse(l1) && is_sparse(l2)) {
  1853. GMM_WARNING3("Inefficient row matrix - col matrix mult for "
  1854. "sparse matrices, using temporary");
  1855. mult_row_col_with_temp(l1, l2, l3,
  1856. typename principal_orientation_type<typename
  1857. linalg_traits<L3>::sub_orientation>::potype());
  1858. }
  1859. else {
  1860. typename linalg_traits<L2>::const_col_iterator
  1861. it2b = linalg_traits<L2>::col_begin(l2), it2,
  1862. ite = linalg_traits<L2>::col_end(l2);
  1863. size_type i,j, k = mat_nrows(l1);
  1864. for (i = 0; i < k; ++i) {
  1865. typename linalg_traits<L1>::const_sub_row_type r1=mat_const_row(l1, i);
  1866. for (it2 = it2b, j = 0; it2 != ite; ++it2, ++j)
  1867. l3(i,j) = vect_sp(r1, linalg_traits<L2>::col(it2));
  1868. }
  1869. }
  1870. }
  1871. // row - row matrix-matrix mult
  1872. template <typename L1, typename L2, typename L3> inline
  1873. void mult_spec(const L1& l1, const L2& l2, L3& l3, r_mult) {
  1874. mult_spec(l1, l2, l3,r_mult(),typename linalg_traits<L1>::storage_type());
  1875. }
  1876. template <typename L1, typename L2, typename L3>
  1877. void mult_spec(const L1& l1, const L2& l2, L3& l3, r_mult, abstract_dense) {
  1878. // optimizable
  1879. clear(l3);
  1880. size_type nn = mat_nrows(l3), mm = mat_nrows(l2);
  1881. for (size_type i = 0; i < nn; ++i) {
  1882. for (size_type j = 0; j < mm; ++j) {
  1883. add(scaled(mat_const_row(l2, j), l1(i, j)), mat_row(l3, i));
  1884. }
  1885. }
  1886. }
  1887. template <typename L1, typename L2, typename L3>
  1888. void mult_spec(const L1& l1, const L2& l2, L3& l3, r_mult, abstract_sparse) {
  1889. // optimizable
  1890. clear(l3);
  1891. size_type nn = mat_nrows(l3);
  1892. for (size_type i = 0; i < nn; ++i) {
  1893. typename linalg_traits<L1>::const_sub_row_type rl1=mat_const_row(l1, i);
  1894. typename linalg_traits<typename linalg_traits<L1>::const_sub_row_type>::
  1895. const_iterator it = vect_const_begin(rl1), ite = vect_const_end(rl1);
  1896. for (; it != ite; ++it)
  1897. add(scaled(mat_const_row(l2, it.index()), *it), mat_row(l3, i));
  1898. }
  1899. }
  1900. template <typename L1, typename L2, typename L3>
  1901. void mult_spec(const L1& l1, const L2& l2, L3& l3, r_mult, abstract_skyline)
  1902. { mult_spec(l1, l2, l3, r_mult(), abstract_sparse()); }
  1903. // col - col matrix-matrix mult
  1904. template <typename L1, typename L2, typename L3> inline
  1905. void mult_spec(const L1& l1, const L2& l2, L3& l3, c_mult) {
  1906. mult_spec(l1, l2,l3,c_mult(),typename linalg_traits<L2>::storage_type(),
  1907. typename linalg_traits<L2>::sub_orientation());
  1908. }
  1909. template <typename L1, typename L2, typename L3, typename ORIEN>
  1910. void mult_spec(const L1& l1, const L2& l2, L3& l3, c_mult,
  1911. abstract_dense, ORIEN) {
  1912. typedef typename linalg_traits<L2>::value_type T;
  1913. size_type nn = mat_ncols(l3), mm = mat_ncols(l1);
  1914. for (size_type i = 0; i < nn; ++i) {
  1915. clear(mat_col(l3, i));
  1916. for (size_type j = 0; j < mm; ++j) {
  1917. T b = l2(j, i);
  1918. if (b != T(0)) add(scaled(mat_const_col(l1, j), b), mat_col(l3, i));
  1919. }
  1920. }
  1921. }
  1922. template <typename L1, typename L2, typename L3, typename ORIEN>
  1923. void mult_spec(const L1& l1, const L2& l2, L3& l3, c_mult,
  1924. abstract_sparse, ORIEN) {
  1925. // optimizable
  1926. clear(l3);
  1927. size_type nn = mat_ncols(l3);
  1928. for (size_type i = 0; i < nn; ++i) {
  1929. typename linalg_traits<L2>::const_sub_col_type rc2=mat_const_col(l2, i);
  1930. typename linalg_traits<typename linalg_traits<L2>::const_sub_col_type>::
  1931. const_iterator it = vect_const_begin(rc2), ite = vect_const_end(rc2);
  1932. for (; it != ite; ++it)
  1933. add(scaled(mat_const_col(l1, it.index()), *it), mat_col(l3, i));
  1934. }
  1935. }
  1936. template <typename L1, typename L2, typename L3>
  1937. void mult_spec(const L1& l1, const L2& l2, L3& l3, c_mult,
  1938. abstract_sparse, row_major) {
  1939. typedef typename linalg_traits<L2>::value_type T;
  1940. GMM_WARNING3("Inefficient matrix-matrix mult for sparse matrices");
  1941. clear(l3);
  1942. size_type mm = mat_nrows(l2), nn = mat_ncols(l3);
  1943. for (size_type i = 0; i < nn; ++i)
  1944. for (size_type j = 0; j < mm; ++j) {
  1945. T a = l2(i,j);
  1946. if (a != T(0)) add(scaled(mat_const_col(l1, j), a), mat_col(l3, i));
  1947. }
  1948. }
  1949. template <typename L1, typename L2, typename L3, typename ORIEN> inline
  1950. void mult_spec(const L1& l1, const L2& l2, L3& l3, c_mult,
  1951. abstract_skyline, ORIEN)
  1952. { mult_spec(l1, l2, l3, c_mult(), abstract_sparse(), ORIEN()); }
  1953. // col - row matrix-matrix mult
  1954. template <typename L1, typename L2, typename L3> inline
  1955. void mult_spec(const L1& l1, const L2& l2, L3& l3, crmult)
  1956. { mult_spec(l1,l2,l3,crmult(), typename linalg_traits<L1>::storage_type()); }
  1957. template <typename L1, typename L2, typename L3>
  1958. void mult_spec(const L1& l1, const L2& l2, L3& l3, crmult, abstract_dense) {
  1959. // optimizable
  1960. clear(l3);
  1961. size_type nn = mat_ncols(l1), mm = mat_nrows(l1);
  1962. for (size_type i = 0; i < nn; ++i) {
  1963. for (size_type j = 0; j < mm; ++j)
  1964. add(scaled(mat_const_row(l2, i), l1(j, i)), mat_row(l3, j));
  1965. }
  1966. }
  1967. template <typename L1, typename L2, typename L3>
  1968. void mult_spec(const L1& l1, const L2& l2, L3& l3, crmult, abstract_sparse) {
  1969. // optimizable
  1970. clear(l3);
  1971. size_type nn = mat_ncols(l1);
  1972. for (size_type i = 0; i < nn; ++i) {
  1973. typename linalg_traits<L1>::const_sub_col_type rc1=mat_const_col(l1, i);
  1974. typename linalg_traits<typename linalg_traits<L1>::const_sub_col_type>::
  1975. const_iterator it = vect_const_begin(rc1), ite = vect_const_end(rc1);
  1976. for (; it != ite; ++it)
  1977. add(scaled(mat_const_row(l2, i), *it), mat_row(l3, it.index()));
  1978. }
  1979. }
  1980. template <typename L1, typename L2, typename L3> inline
  1981. void mult_spec(const L1& l1, const L2& l2, L3& l3, crmult, abstract_skyline)
  1982. { mult_spec(l1, l2, l3, crmult(), abstract_sparse()); }
  1983. /* ******************************************************************** */
  1984. /* Symmetry test. */
  1985. /* ******************************************************************** */
  1986. ///@endcond
  1987. /** test if A is symmetric.
  1988. @param A a matrix.
  1989. @param tol a threshold.
  1990. */
  1991. template <typename MAT> inline
  1992. bool is_symmetric(const MAT &A, magnitude_of_linalg(MAT) tol
  1993. = magnitude_of_linalg(MAT)(-1)) {
  1994. typedef magnitude_of_linalg(MAT) R;
  1995. if (tol < R(0)) tol = default_tol(R()) * mat_maxnorm(A);
  1996. if (mat_nrows(A) != mat_ncols(A)) return false;
  1997. return is_symmetric(A, tol, typename linalg_traits<MAT>::storage_type());
  1998. }
  1999. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  2000. template <typename MAT>
  2001. bool is_symmetric(const MAT &A, magnitude_of_linalg(MAT) tol,
  2002. abstract_dense) {
  2003. size_type m = mat_nrows(A);
  2004. for (size_type i = 1; i < m; ++i)
  2005. for (size_type j = 0; j < i; ++j)
  2006. if (gmm::abs(A(i, j)-A(j, i)) > tol) return false;
  2007. return true;
  2008. }
  2009. template <typename MAT>
  2010. bool is_symmetric(const MAT &A, magnitude_of_linalg(MAT) tol,
  2011. abstract_sparse) {
  2012. return is_symmetric(A, tol, typename principal_orientation_type<typename
  2013. linalg_traits<MAT>::sub_orientation>::potype());
  2014. }
  2015. template <typename MAT>
  2016. bool is_symmetric(const MAT &A, magnitude_of_linalg(MAT) tol,
  2017. row_major) {
  2018. for (size_type i = 0; i < mat_nrows(A); ++i) {
  2019. typedef typename linalg_traits<MAT>::const_sub_row_type row_type;
  2020. row_type row = mat_const_row(A, i);
  2021. typename linalg_traits<row_type>::const_iterator
  2022. it = vect_const_begin(row), ite = vect_const_end(row);
  2023. for (; it != ite; ++it)
  2024. if (gmm::abs(*it - A(it.index(), i)) > tol) return false;
  2025. }
  2026. return true;
  2027. }
  2028. template <typename MAT>
  2029. bool is_symmetric(const MAT &A, magnitude_of_linalg(MAT) tol,
  2030. col_major) {
  2031. for (size_type i = 0; i < mat_ncols(A); ++i) {
  2032. typedef typename linalg_traits<MAT>::const_sub_col_type col_type;
  2033. col_type col = mat_const_col(A, i);
  2034. typename linalg_traits<col_type>::const_iterator
  2035. it = vect_const_begin(col), ite = vect_const_end(col);
  2036. for (; it != ite; ++it)
  2037. if (gmm::abs(*it - A(i, it.index())) > tol) return false;
  2038. }
  2039. return true;
  2040. }
  2041. template <typename MAT>
  2042. bool is_symmetric(const MAT &A, magnitude_of_linalg(MAT) tol,
  2043. abstract_skyline)
  2044. { return is_symmetric(A, tol, abstract_sparse()); }
  2045. ///@endcond
  2046. /** test if A is Hermitian.
  2047. @param A a matrix.
  2048. @param tol a threshold.
  2049. */
  2050. template <typename MAT> inline
  2051. bool is_hermitian(const MAT &A, magnitude_of_linalg(MAT) tol
  2052. = magnitude_of_linalg(MAT)(-1)) {
  2053. typedef magnitude_of_linalg(MAT) R;
  2054. if (tol < R(0)) tol = default_tol(R()) * mat_maxnorm(A);
  2055. if (mat_nrows(A) != mat_ncols(A)) return false;
  2056. return is_hermitian(A, tol, typename linalg_traits<MAT>::storage_type());
  2057. }
  2058. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  2059. template <typename MAT>
  2060. bool is_hermitian(const MAT &A, magnitude_of_linalg(MAT) tol,
  2061. abstract_dense) {
  2062. size_type m = mat_nrows(A);
  2063. for (size_type i = 1; i < m; ++i)
  2064. for (size_type j = 0; j < i; ++j)
  2065. if (gmm::abs(A(i, j)-gmm::conj(A(j, i))) > tol) return false;
  2066. return true;
  2067. }
  2068. template <typename MAT>
  2069. bool is_hermitian(const MAT &A, magnitude_of_linalg(MAT) tol,
  2070. abstract_sparse) {
  2071. return is_hermitian(A, tol, typename principal_orientation_type<typename
  2072. linalg_traits<MAT>::sub_orientation>::potype());
  2073. }
  2074. template <typename MAT>
  2075. bool is_hermitian(const MAT &A, magnitude_of_linalg(MAT) tol,
  2076. row_major) {
  2077. for (size_type i = 0; i < mat_nrows(A); ++i) {
  2078. typedef typename linalg_traits<MAT>::const_sub_row_type row_type;
  2079. row_type row = mat_const_row(A, i);
  2080. typename linalg_traits<row_type>::const_iterator
  2081. it = vect_const_begin(row), ite = vect_const_end(row);
  2082. for (; it != ite; ++it)
  2083. if (gmm::abs(gmm::conj(*it) - A(it.index(), i)) > tol) return false;
  2084. }
  2085. return true;
  2086. }
  2087. template <typename MAT>
  2088. bool is_hermitian(const MAT &A, magnitude_of_linalg(MAT) tol,
  2089. col_major) {
  2090. for (size_type i = 0; i < mat_ncols(A); ++i) {
  2091. typedef typename linalg_traits<MAT>::const_sub_col_type col_type;
  2092. col_type col = mat_const_col(A, i);
  2093. typename linalg_traits<col_type>::const_iterator
  2094. it = vect_const_begin(col), ite = vect_const_end(col);
  2095. for (; it != ite; ++it)
  2096. if (gmm::abs(gmm::conj(*it) - A(i, it.index())) > tol) return false;
  2097. }
  2098. return true;
  2099. }
  2100. template <typename MAT>
  2101. bool is_hermitian(const MAT &A, magnitude_of_linalg(MAT) tol,
  2102. abstract_skyline)
  2103. { return is_hermitian(A, tol, abstract_sparse()); }
  2104. ///@endcond
  2105. }
  2106. #endif // GMM_BLAS_H__